Sign in to follow this  

practicing file IO

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
Quote:
Original post by nectron101
It 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.

Share this post


Link to post
Share on other sites
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;
}


thanks in advance

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

Share this post


Link to post
Share on other sites
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.

Your responses mean a lot!

Thanks

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites

This topic is 4393 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this