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

  1. #1
    Registered User
    Join Date
    May 2013
    Posts
    2

    Question 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. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    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.
    Last edited by Malcolm McLean; 05-04-2013 at 07:37 AM.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    May 2013
    Posts
    2
    i got only one lesson(2hr) per week......

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Quote Originally Posted by C++shiftman View Post
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by rcgldr View Post
    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!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    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. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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);
    Last edited by Elysia; 05-04-2013 at 02:21 PM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by Elysia View Post
    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 subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 03-21-2013, 03:31 PM
  2. Replies: 3
    Last Post: 11-28-2012, 09:16 AM
  3. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  4. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  5. Reading flat file and generating tagged file
    By AngKar in forum C# Programming
    Replies: 4
    Last Post: 03-24-2006, 08:29 AM

Tags for this Thread