Thread: C++ read file ?

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    C++ read file ?

    Hello,

    How do you read a file in C++?
    For example, I have a text file data.txt contains:

    hammer#12.20
    saw#20.50
    shovel#9.25

    Now I need to write a program to read all the data in data.txt and totals the price of all items. The output print out should look like:

    hammer 12.20
    saw 20.50
    shovel 9.25
    Total: 71.2

    I haven't study array yet, just use loops to write this program. This is my code, but It's not work.
    PHP Code:
    -------------------------------------------------------
    #include <iostream>
    #include <fstream>

    using namespace std
    int mainvoid )
    {
    string newPart;
    char ch;

    ifstream inData;
    inData.open("data.txt" );

    inData.get(ch);

    while (!
    inData.eof()) {

    while(
    ch != '#') {
    newPart += ch;
    }

    inData.get(ch);
    }

    cout << newPart;

    inData.close();

    return 
    0;
    }
    ------------------------------------------------- 


    Please help me! Thanks

  2. #2
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    while(ch != '#') {
    newPart += ch;
    }

    Say you read in 'h' for the first char. How many times does that loop execute?

    You can use a version of get() or getline() that will do most of the work for you. I would use the version of getline() that takes three parameters: (a char array name, the size of the array, the delimiter) Specify the delimiter as '#', and getline will read all the char's up to the # sign into the array, and then it will discard the # sign. Then you can use the >> operator to read in what's to the right of the pound sign into a variable of type double like this:

    double price = 0;
    inFile>>price;

    The >> operator will keep reading until it hits whitespace(=carriage returns, tabs, spaces). Check here for some good info:

    http://www.augustcouncil.com/~tgibso...al/iotips.html

    Make sure to read #4 which is a good explanation of how streams work and alerts you to the problems you can have when you use the >>operator and getline() in sequence to read in data. In your case the delimiter in getline() is the # sign, so the problem mentioned in 4 is avoided.
    Last edited by 7stud; 05-05-2003 at 08:33 PM.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    3
    That's I have problem in there
    If I write:

    while (!inData.eof()) {

    cout<<ch;

    inData.get(ch);

    }

    It'll work through the file and print each character until the end of the file. I need to seperate the name(hammer) and the price(12.20) but I don't know how to do that.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    lol. As always seems to be the case, I was editing my post when you replied. Re-read it and see if there's anymore useful information.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    3
    thanks, but I haven't study array yet. The assignment is using the loop to read each character in a line and store them to a variable (newPart += ch) until it hits the pound (#) sign, and then read the number.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    inData.get(ch);

    while(ch != '#') {
    newPart += ch;
    }

    Say you read in 'h' for the first char. How many times does that loop execute?
    Back to this part then. My point is not that you shouldn't have that while loop but that what you do inside it has to be changed. That loop is an infinite loop given your input. It will read in the first char 'h', and since it doesn't equal '#', it adds it to the variable newPart, but then the loop goes back up and checks the condition, and since ch hasn't changed, it still doesn't equal '#', so it adds 'h' to newPart again, and it will do that an infinite amount of times because you never change ch inside the loop. You have to change it to read in the next char in the loop.

    You can't just move the read statement into the loop either:
    Code:
    while(ch != '#')
    {
       inData.get(ch);
       newPart += ch;
    }
    because when you hit the EOF, you're going to have problems with it repeating the last char. So, you need to do something like this:
    Code:
    inData.get(ch);
    
    while(ch != '#')
    {
        newPart += ch;
        inData.get(ch)
    }
    Last edited by 7stud; 05-06-2003 at 02:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM