![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2007
Posts: 1
Rep Power: 0
![]() |
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();
} |
|
|
|
|
|
#2 |
|
Programmer
|
What do you mean "couldnt do that"? Your code looks like it should work.
|
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 262
Rep Power: 2
![]() |
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. Last edited by Fall Back Son; Jun 19th, 2007 at 2:13 AM. |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
Post any errors that your compiler shows, if there is any. Maybe the conio.h doesn`t support the clrscr() function.
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jun 2007
Location: askexpert.info
Posts: 4
Rep Power: 0
![]() |
open the file in write mode.
__________________
Programming Tutorials |Interview Question And Answer Freelance Web Designer|Freelance Programmer |
|
|
|
|
|
#6 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
cheap freelancer, please review the forum's rules regarding commercial signatures.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#7 |
|
Professional Programmer
Join Date: Mar 2005
Location: Student of University of Mumbai, Maharashtra State, India
Posts: 344
Rep Power: 4
![]() |
conio.h supports clrscr(); within Turbo C++ 3.0
@OP: please post any errors that you encounter while compiling, as mentioned earlier.
__________________
Visit: http://www.somaiya.edu |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Nov 2006
Location: 163H
Posts: 213
Rep Power: 2
![]() |
By the way how do you know what compiler is the OP using????
__________________
You never test the depth of a river with both feet. The believer is happy. The doubter is wise. Free speech carries with it some freedom to listen. The next generation will always surpass the previous one. It`s one of the never ending cycles of life. |
|
|
|
|
|
#9 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,034
Rep Power: 5
![]() |
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");fprintf(fp,"\n HI"); 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++.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot. - Vaarsuvius, Order of the Stick |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problem processing file into a char array | csrocker101 | C++ | 1 | May 8th, 2007 11:50 PM |
| converting string to float | beginnerCCC | C | 22 | Oct 2nd, 2006 11:59 PM |
| OnlineTextEditor.Com! | Sane | Show Off Your Open Source Projects | 43 | Jun 16th, 2006 8:55 AM |
| After execution - Error cannot locate /Skin File? | wchar | Visual Basic | 1 | Mar 5th, 2005 9:04 PM |
| airport Log program using 3D linked List : problem reading from file | gemini_shooter | C++ | 0 | Mar 2nd, 2005 4:12 PM |