• Welcome to Overclockers Forums! Join us to reply in threads, receive reduced ads, and to customize your site experience!

Simple C question

Overclockers is supported by our readers. When you click a link to make a purchase, we may earn a commission. Learn More.

grimm003

Member
Joined
Jul 20, 2004
Location
SIU
Hi, I have a C project I am working on, and my t.a. is not much help. The point of the program is to open a .txt file that has names and phone numbers(suposed to be a telephone directory program). I am having trouble opening the file.

my t.a. said to use this:
fp = fopen("a.txt", "r+");

but that gets compiling errors that fp is undefined. I looked on the web a little and came across "FILE'' so I tried

FILE fp;
fp = fopen("a.txt", "r+");

but no luck. I got it to compile by just using

fopen("a.txt", "r+");

but how do I access it to read and write? Thank you for any help
 
nevermind, found it. But if any1 has a link to a good site that can explain the funtions I will need to search, read, and write, it would be much appreciated. I am only familiar with java, but my teacher thinks we know C/C++.
 
There is this strange thing called "a pointer" in C, which Java lacks.

FILE is a structure, and fp will be a pointer, pointing to that structure.

FILE *fp;
fp = fopem (...);

Notice the * in front of fp. And get yourself a C book which explains pointers among othre things.

As for the functions you need, fopen, fread, fwrite, fseek, fclose are all the right ones. Look at http://www.cppreference.com/stdio/fopen.html for example.
 
thanks for your help. I sort of know about pointers, but it wasnt explained too much to us, just that they exist. I'm sure a C book would help, but this is just a small section of a larger class, and we only had 1 other project to write, and this is our last one. So it would be pointless for me to buy the book now. thanks for the link, that should be what I need.
 
If you ever do decide to go farther with C++ (rather than C), I'd highly recommend "C++ In Plain English". The major systems calls are all described with examples. It's in the programming reference sticky. -- Paul
 
I would recommend not using file pointers anymore. The preferred method in C++ now is streams. You create a file stream and then call member functions to open the file. Streams do not exist in C of course, and file pointers are necessary.

My personal favorite book for new C++ learners is the one by walter savitch. We used it in college, and it's very easy to learn from and assumes nothing.
 
www.cplusplus.com

I think that's the best reference for standard cpp stuff out there. Helps me out a lot whenever i get stuck. Also, if you're doing win32 programming, then MSDN is also an excellent reference.
 
Back