# Thread: Reading txt file from C++ and.....

1. ## Reading txt file from C++ and.....

i learnt C++ for 3 months, i only got limited knowledge on C++
now ,i hv to write a C++ program by reading txt file from C++

eg
content of txt file included :

Peter male math a chem b Chinese c
Mary female math c chem a Chinese b

and i hv to sort their name by a
lphabetical order (a to z)
then calculate total marks of subjects.

such as ...
weight of subjects : math 30%,chem 30%, Chinese 40%
marks of subjects: a=10 b=8 c=6
for Peter;
10x0.3 + 8x0.3+6x0.4 = total marks

also ,i need to sort it by total marks

i don't know how to write it.
is there anyone can help me?

2. You need a struct record, which contains all the data in one line (name, sex, math mark, etc). Then you need to write a function to fill a struct record from a line. Then write a higher-level function to read the whole file into an array of records.
Then sort the records by name with qsort. Then calculate the average scores for each name and print out report 1. Then qsort again on the average score, and print out report 2.

3. Moved to C++ since that is mentioned 6 times already.

I fixed the thread title as well.

> i learnt C++ for 3 months, i only got limited knowledge on C++
I find it hard to believe that after 3 months of study, this is your first attempt at reading a file.

Your first exercise is to read the file into memory, and then print it out to the screen.
If you can't "print what you read" with any reliability, it's a waste of time working on the rest of the problem (you're just going to confuse yourself by processing garbage).

4. i got only one lesson(2hr) per week......

5. Originally Posted by C++shiftman
i got only one lesson(2hr) per week......
Well if you do no other practice outside of class, you should just give up programming.

It doesn't matter what you do (music, sport, anything), you're never going to be any good at it if you don't put in the hours. At your rate, it would take you 100 YEARS to achieve mastery of the subject.

Regardless of however much time you allow per week, you should be able to at least post an attempt.

No doubt file handling was discussed in the most recent 2hr class, so start by reading your notes again.

Start simple - open the file and read the first line, and print it.

We can't tell you where you're going wrong, or what to do next, and we're not going to just give you the answer.

6. One issue is that the text file records vary in size. Assuming you will use a structure for the fields of each line, you'll need to make sure that each field within the structure is large enough to hold the largest possible name. For example, country: "The Former Yugoslav Republic of Macedonia" (36 letters plus the null at the end), there may be longer examples, but mostly you should examine the text file(s) you'll be using to get reasonable maximum field sizes.

For the program, you can use fgets() to read text files one line at a time, then use strtok() if there are multiple delimiters, or strchr() if all delimiters are the same character, such as a space.

7. Originally Posted by rcgldr
For the program, you can use fgets() to read text files one line at a time, then use strtok() if there are multiple delimiters, or strchr() if all delimiters are the same character, such as a space.
In C++, we prefer to use std::string and std::getline to read lines. Also supports specifying the "end" of a line, so you can get it to stop reading when hitting a specific character, emulating strtok and strchr which we prefer not to use in C++ (they are C functions!).

8. I understand using getline, but how do you emulate strtok, for example what if the delimeters could be any of space, tab, comma, ..., newline?

9. The third parameter of getline is often overlooked: std::getline - cppreference.com
Don't know what type of delimiter it will be? Then just use getline to get the line and use std::find_first_of.

Another handy way is boost:

std::vector<std::string> words;
std::string s;
boost::split(words, s, boost::is_any_of(", "), boost::token_compress_on);

10. Originally Posted by Elysia
Don't know what type of delimiter it will be? Then just use getline to get the line and use std::find_first_of.
That's the one I was looking for, thanks.

Popular pages Recent additions