Problem with File I/O
Problem with File I/O
I have been trying to write a simple program that opens a file all day long. For some reason it doesnt want to open my file. I have literaly spent the day looking over Dev Shed forums for my answer and each time I try code from someones post, it just doesent work for me. The file I am trying to open is in the same directory as the executable. What could be wrong?
[attachment deleted by admin]
[attachment deleted by admin]
Re:Problem with File I/O
If you added a call to perror immediately after the call to fopen, you'd know exactly what was wrong.
Re:Problem with File I/O
I have added perror and it says "no such file or directory". I am compiling this under VC++6.0 and it puts the executable file in a debug folder. So if I put the file Im trying to open in that same file, I get that error. If I put the file somewhere else, it loads but nothing opens.
What could be the problem?
What could be the problem?
Re:Problem with File I/O
You have to have a file of the correct name in the workig directory, which in the case of VC++, is the dfault directory for you source code. Obviously, fopen() using the read-only setting ("r") won't create a new file, as there still wouldn't be anything to read in it even if it did.
OTOH, given the program above, nothing should happen if it successfully opens the file; the program simply quits on a successful fopen() (note that you really ought to run fclose(), rather than assuming that system closes the file on program shutdown, just as a matter of good coding habits). The best way to demonstrate that no error occurs is to print a part (or all) of the file in question, as in this modified version:
Of course, this is just as a way of testing the code; I have no idea what your actual goal is. Presumably, you know what the program is supposed to read from the file, and can show it in some appropriate manner.
OTOH, given the program above, nothing should happen if it successfully opens the file; the program simply quits on a successful fopen() (note that you really ought to run fclose(), rather than assuming that system closes the file on program shutdown, just as a matter of good coding habits). The best way to demonstrate that no error occurs is to print a part (or all) of the file in question, as in this modified version:
Code: Select all
#include <stdio.h>
#include <stdlib.h>
#define BUF_SZ 255
int main(void)
{
FILE *fp;
char filename[] = "test.txt";
char buffer[BUF_SZ];
// Open file
fp = fopen(filename, "r");
if(NULL == fp)
{
printf("\nCannot open %s\n", filename);
perror(NULL);
}
else
{
fgets(buffer, BUF_SZ, fp);
while (! (NULL == (void *) buffer[0]))
{
puts(buffer);
buffer[0] = (char) NULL;
fgets(buffer, BUF_SZ, fp);
}
fclose(fp);
}
}
Re:Problem with File I/O
When you run a program in the VC++ debugger, the current directory is set to the project directory. So even if your EXE is in C:\project\Debug\project.exe, it will be running in C:\project, and it looks for the file there.beyondsociety wrote: I am compiling this under VC++6.0 and it puts the executable file in a debug folder.
Re:Problem with File I/O
That works as I see what I was doing wrong. Thanks.
I want to be able to enter in any filename and have the program open it and read it. How would I do this?
I want to be able to enter in any filename and have the program open it and read it. How would I do this?
Re:Problem with File I/O
1. Open your "How To Write C Programs" book
2. Read the section which tells you how to do this:
2. Read the section which tells you how to do this:
Code: Select all
char filename[256];
printf("Enter a file name: ");
fflush(stdout);
fgets(filename, 256, stdin);
Re:Problem with File I/O
Sorry about that, I should know better to not ask until I have looked at my book, shame on me. ;D
Why is it better to use fflush than scanf?
Why is it better to use fflush than scanf?
Re:Problem with File I/O
You could use scanf instead of fgets here. I used fflush because I don't think scanf is guaranteed to flush stdout -- stdin and stdout are separate streams, so scanf only knows about stdin.
Note that if you use scanf, you still need to specify the maximum length of the string, or you risk buffer overflow (bad!). The buffer length is the second parameter to fgets but I can't see how to specify that with scanf.
Note that if you use scanf, you still need to specify the maximum length of the string, or you risk buffer overflow (bad!). The buffer length is the second parameter to fgets but I can't see how to specify that with scanf.