Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C (http://www.programmingforums.org/forum60.html)
-   -   Problem with opening a file (http://www.programmingforums.org/showthread.php?t=13380)

raghuveerd Jun 19th, 2007 1:32 AM

Problem with opening a file
 
Hi
I am a beginer to C Programming.I was trying to open a .txt file for reading but couldnt do that this is my code..Help me out.
:

#include<stdio.h>
#include<conio.h>
main()
{
FILE *fp;
clrscr();
fp=fopen("raghu.txt","r");
if(fp==NULL)
printf("ERROR IN OPENING THE FILE");
else
{
printf("hi");
fprintf(fp,"\n HI");
}

fclose(fp);
getch();
}


niteice Jun 19th, 2007 2:23 AM

What do you mean "couldnt do that"? Your code looks like it should work.

Fall Back Son Jun 19th, 2007 3:01 AM

This won't fix whatever problem you are having, but make that

int main ()

and

return 0 ; at the end. DaWei said that on here once, so do it! Lol.

this might fix your problem:


Your code looks like it should work to me. You might consider that you won't be able to open a file for reading if that file doesn't exist. I'm not sure if that is the case (it might create a blank file called raghu.txt), but in any case, you shouldn't try to read from a file that doesn't exist. So look in your directory and make sure that there is a file in that directory called raghu.txt .

Also, you made a call to fprintf, and sent the output to "fp". However, fp was used for reading, not for writing. You are trying to write to a file that is open for reading. This will yield unexpected results, I would imagine. If you want to open for reading and writing, use r+ instead.

pegasus001 Jun 19th, 2007 7:03 AM

Post any errors that your compiler shows, if there is any. Maybe the conio.h doesn`t support the clrscr() function.

cheap freelancer Jun 20th, 2007 6:33 AM

open the file in write mode.

DaWei Jun 20th, 2007 7:01 AM

cheap freelancer, please review the forum's rules regarding commercial signatures.

java_roshan Jun 25th, 2007 6:57 AM

Quote:

Originally Posted by pegasus001 (Post 129357)
Maybe the conio.h doesn`t support the clrscr() function.

conio.h supports clrscr(); within Turbo C++ 3.0

@OP: please post any errors that you encounter while compiling, as mentioned earlier.

pegasus001 Jun 25th, 2007 12:46 PM

Quote:

Originally Posted by java_roshan (Post 129626)
conio.h supports clrscr(); within Turbo C++ 3.0

By the way how do you know what compiler is the OP using????

lectricpharaoh Jun 25th, 2007 7:24 PM

You need to decide if you're trying to read from a file, write to a file, or both. As it stands, you're opening the file in read mode:
:

fp=fopen("raghu.txt","r");
You then try using an output function on this file:
:

fprintf(fp,"\n HI");
You want to either change the fopen() to use write mode (or read/write), or change the fprintf() to an input function, like fgets() or fscanf() (depending on your needs).

You might want to check this reference if you don't have documentation for the C standard library. It doesn't have everything, but it's got the essential stuff for C and C++.


All times are GMT -5. The time now is 2:37 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC