CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Feb 2009
    Posts
    1

    using data in a file

    So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:

    Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):

    Code:
    Bailey           M CC 68
    Harrison         F CC 71
    Grant            M UN 75
    How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?

  2. #2
    Join Date
    Mar 2004
    Location
    KL, Malaysia
    Posts
    63

    Re: using data in a file

    How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
    1. Read one row from file and store in a CString variable.
    2. Use CString::Tokenize to extract 2nd and 4th value to local string variables
    3. Compare if 2nd value is 'M', if yes then further convert 4th value to integer
    4. Do further calculations here
    5. Repeat step 1
    Last edited by ckweius; February 19th, 2009 at 10:26 PM.

  3. #3
    Join Date
    Nov 2006
    Location
    Australia
    Posts
    1,569

    Re: using data in a file

    Or if you don't want to use CString, have a look at this thread.
    Good judgment is gained from experience. Experience is gained from bad judgment.
    Cosy Little Game | SDL | GM script | VLD | Syntax Hlt | Can you help me with my homework assignment?

  4. #4
    Join Date
    Feb 2009
    Posts
    7

    Re: using data in a file

    Hi!
    What is wrong with strtok ? it always works fine for me.

    Regards,
    jibeiuvia!

  5. #5
    Join Date
    Jan 2009
    Posts
    5

    Re: using data in a file

    Quote Originally Posted by AtillaTheHun View Post
    So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:

    Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):

    Code:
    Bailey           M CC 68
    Harrison         F CC 71
    Grant            M UN 75
    How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
    Ok, take it easy man!
    if you like fstream, then you have to learn about input stream and output stream,
    I'd prefer stringstream of sstream if I were you.

    -Antonio

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: using data in a file

    Quote Originally Posted by ckweius View Post
    1. Read one row from file and store in a CString variable.
    2. Use CString::Tokenize to extract 2nd and 4th value to local string variables
    3. Compare if 2nd value is 'M', if yes then further convert 4th value to integer
    4. Do further calculations here
    5. Repeat step 1
    This is the non-visual C++ forum.

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: using data in a file

    For a simple input file like that, you can just read
    directly into the appropriate typed variables:

    Code:
    // assuming the ifstream object is : in
    
    string name , sex , code;
    int       num;
    
    while (in >> name >> sex >> code >> num)
    {
         // process the line
    }

    For more complex cases, reading line by line, placing into a stringstream,
    and using it to gte the input is a better way to go (as mentioned in
    previous posts).

    Also, it is usually easier to do error checking in the line by line method.
    Last edited by Philip Nicoletti; February 20th, 2009 at 08:58 AM.

  8. #8
    Join Date
    Feb 2009
    Posts
    1

    Re: using data in a file

    Quote Originally Posted by AtillaTheHun View Post
    So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:

    Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):

    Code:
    Bailey           M CC 68
    Harrison         F CC 71
    Grant            M UN 75
    How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
    You can
    use getlline to read each string into a vector of strings and process each line/string in the vector after that.


    - I am sorry if my help doesnot help you, I feel sorry for that only. It's my happiness to post help you, mustab man on atilla.

  9. #9
    Join Date
    Feb 2009
    Posts
    2

    Re: using data in a file

    Quote Originally Posted by AtillaTheHun View Post
    So I am taking an intro to programming course, and I have one problem that I haven't been able to solve. This is using the <fstream> library:

    Given a text file, I need my program to use the data in it to perform certain operations. I've figured out how to read and echo the file, but I don't know how to use it to perform calculations. Example, I would like to find the average of the numbers in the lines containing 'M' (for male):

    Code:
    Bailey           M CC 68
    Harrison         F CC 71
    Grant            M UN 75
    How would I make my program get just the numeric input from the lines with an 'M', so that I can find their average?
    Hello, I write code in C# only, you can
    read the file into a string easily
    str=File.ReadAllText(filename);
    str.Tokenize(provide delimiters[])
    then you can check match condition eaiser

    You can do it yourself!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured