By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,901 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,901 IT Pros & Developers. It's quick & easy.

opening a file, searching a string, getting data

P: 3
Hello all,

I'm a chemical engineering student. our teacher has given us a project to do some calculations based on some equations. those equations include constants, which are to be read from a file(I don't understand why?) i took an introductory course on c programming but the curriculum didn't include opening files, searching files and getting data from files. i made some searching through google and learned about opening and closing files, but i still don't know how to search for a string and how to get data from the same line as the string.

any help here would be appreciated. Thanks.
Jan 15 '08 #1
Share this Question
Share on Google+
6 Replies


sicarie
Expert Mod 2.5K+
P: 4,677
I personally like this tutorial - it'll refresh you on the basics, as well as provide a reference for whatever File IO stuff you may need.

If you have any trouble coding it, feel free to post, though we do request that you take a gander at the Posting Guidelines.

Good luck!
Jan 15 '08 #2

P: 3
I personally like this tutorial - it'll refresh you on the basics, as well as provide a reference for whatever File IO stuff you may need.

If you have any trouble coding it, feel free to post, though we do request that you take a gander at the Posting Guidelines.

Good luck!

I did look at the guidelines, and the program I'll write actually is a lot longer than the searching function. : )

thanks for the tutorial, but I don't know c++, I was inquiring about c. are there any sources for c tutorials like this one?
Jan 15 '08 #3

sicarie
Expert Mod 2.5K+
P: 4,677
Ah, right, sorry about that - missed that part. Does this look like it will help?
Jan 15 '08 #4

P: 3
Ah, right, sorry about that - missed that part. Does this look like it will help?
I looked at that one before, and there are actually questions in my mind about this one.

When I'm reading from a file, where do i put the file and what do i write for the filename part when using fopen(filename, mode)? for example, if my file is constants.txt, where should it be located for the program to see it and what should i write for the filename part?

in addition, this does not provide information about the behaviour of the functions fscanf and fprintf. for instance, in the while loop, it says to go on until end of file is reached. however, how does it pass to the next line in the file? does fscanf do it automatically?

i thought of that source to be inadequate. sorry to be bothering you so much but i looked at first 2 or 3 pages of google search when i type "c file open", "c fopen", "c search file" and "c file access" (searching each separately). any other help would be wonderful. : )

thanks, by the way : )
Jan 15 '08 #5

gpraghuram
Expert 100+
P: 1,275
I looked at that one before, and there are actually questions in my mind about this one.

When I'm reading from a file, where do i put the file and what do i write for the filename part when using fopen(filename, mode)? for example, if my file is constants.txt, where should it be located for the program to see it and what should i write for the filename part?

in addition, this does not provide information about the behaviour of the functions fscanf and fprintf. for instance, in the while loop, it says to go on until end of file is reached. however, how does it pass to the next line in the file? does fscanf do it automatically?

i thought of that source to be inadequate. sorry to be bothering you so much but i looked at first 2 or 3 pages of google search when i type "c file open", "c fopen", "c search file" and "c file access" (searching each separately). any other help would be wonderful. : )

thanks, by the way : )

fopen takes two parameters
first is the file name(which u can specify the full path) and second is the mode
that is write or read etc.
When reading u can use fgets and every time you use fgets it automatically goes to the next line which is hanled by the System

Raghuram
Jan 15 '08 #6

sicarie
Expert Mod 2.5K+
P: 4,677
There's also our tutorial on C File IO.
Jan 16 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.