# practicing file IO

## Recommended Posts

nectron101    100
I'm trying to practice on file input and output.. I've created file called inputfile.in using notepad and only wrote the number 4 inside it. I tried to make a program to read the number stored inside inputfile.in but unfortunately, it doesn't work.. It always enters the if function.. What am I doing wrong?
#include <stdio.h>
#include <stdlib.h>

#define INPUT_FILE "inputfile.in"

int main()
{
FILE *infile;
infile= fopen(INPUT_FILE, "r");

if(infile == NULL )
{
printf("Cannot open file, " INPUT_FILE "\n");
printf("Terminating program...\n");
system ("PAUSE");
}

fprintf(infile, "Testing...\n");

system ("PAUSE");
return 0;
}


##### Share on other sites
pulpfist    528
Are you sure the inputfile.in is in the current directory?
And that the filename is correct?

##### Share on other sites
nectron101    100
It is in the same directory where the code is saved, compiled, and run..

It keeps telling me that the file cannot be opened

##### Share on other sites
pulpfist    528
If you want to read the file try this
#include <stdio.h>#include <stdlib.h>#define INPUT_FILE "inputfile.in"int main(){ char buffer[256]; FILE *infile; infile= fopen(INPUT_FILE, "r"); if(infile == NULL ) {  printf("Cannot open file, " INPUT_FILE "\n");  printf("Terminating program...\n");  return 1; } fgets(buffer, 256, infile); printf(buffer); fclose(infile); fgetc(stdin); return 0;}

##### Share on other sites
darookie    1441
Quote:
 Original post by nectron101It is in the same directory where the code is saved, compiled, and run..It keeps telling me that the file cannot be opened

If you are using the Visual Studio IDE, make sure that the input file resides in the same directory as the project file. That will be the default working directory VC++ uses when you run your program from within the IDE.

pulpfist    528

##### Share on other sites
nectron101    100
it still enters the if statement to terminate the program..

Is my if statement correct ?

The file i have is in the same directory and this is the code I'm doing now..

I still can't get it open the file and read from it.

#include <stdio.h>#include <stdlib.h>#define INPUT_FILE "inputfile2.in"int main(){ char buffer[256]; FILE *infile; infile= fopen(INPUT_FILE, "r"); if(infile == NULL ) {  printf("Cannot open file, " INPUT_FILE "\n");  printf("Terminating program...\n");   system("PAUSE"); } fgets(buffer, 256, infile); printf(buffer); fclose(infile); fgetc(stdin); system("PAUSE"); return 0;}

[Edited by - nectron101 on December 4, 2005 5:25:34 PM]

##### Share on other sites
nectron101    100
Sorry to double post..

But I'm working on a project that uses file input and output, and I must understand the concept and be able to apply it on my code.

Thanks

##### Share on other sites
pulpfist    528
Everything look good, by the way, the fgetc(stdin) was supposed to replace the pesky system("PAUSE") thing, but if you prefer it, thats up to you.

As darookie says, VC++ typically creates a new folder for each project called Debug. It places its executable target (the exe file) in this folder.
If this is the case, try to place the infile.in in the Debug folder, in other words, in the same folder as the <my prog>.exe file resides.

You can also give your program the full path to the file, to be 100% sure the program finds it.

something like
infile = fopen("C:\\myprogs\\myfolder\\infile.in", "r");

##### Share on other sites
aloknarula    122
I tested the code works fine. You are not placing the file in proper location.