Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: Reading in a file in C++

  1. #1
    Join Date
    Jan 2013
    Beans
    17

    Reading in a file in C++

    I am trying to read in a file that is similar to this:

    push 10
    pop
    push 20
    push 30
    pop
    push 10
    push 50
    push 20
    push 20
    pop

    I am trying to make it so if it says push then it uses the pushfunction to push 10. And when it says pop it will pop off the top of the stack. Etc...

    When you read in a file how do you make it read in the string, then use the integer?

    Code:
    	getline(cin, fileAddress);
    	ifstream textFile (fileAddress);
    
    	//Tests the file
    	if (textFile.fail())
    	{
    		cout << "File was not found"<< endl;
    		system("PAUSE");
    		exit (1);
    	}
    	else
    		cout << "\nFile opened successfully" << endl;
    That is what I have to read in the code but I am just stuck on how to store it then use it.

    Thanks for the help!

  2. #2
    Join Date
    Apr 2012
    Beans
    7,256

    Re: Reading in a file in C++


  3. #3
    Join Date
    Jan 2013
    Beans
    17

    Re: Reading in a file in C++

    Quote Originally Posted by steeldriver View Post
    So you are saying that with each line of file that I read I should do a line of code like:

    Code:
    data >> stringVariable >> intVariable

  4. #4
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Reading in a file in C++

    Quote Originally Posted by Amulon View Post
    So you are saying that with each line of file that I read I should do a line of code like:

    Code:
    data >> stringVariable >> intVariable
    Yes, something like that. Providing of course that 'data' is a non-blocking stream.

    For example:
    Code:
    fstream file("data.txt", fstream::in);
    
    if (file)
    {
        while (!file.eof())
        {
            string data;
    
            getline(file, data);
    
            if (file)
            {
                istringstream iss(data);
                string op;
                int value;  
    
                iss >> op >> value;
    
    
                // ...
            }
        }
    }
    Last edited by dwhitney67; January 29th, 2013 at 12:54 PM.

  5. #5
    Join Date
    Jan 2013
    Beans
    17

    Re: Reading in a file in C++

    So I have used your insights to pretty much finish the project. Everything is good until 1 spot. The file is like:

    push 10
    push 10
    push 10
    push 10
    pop
    push 2
    push 2
    push 2

    And when it doesnt read any data (other than the string) on the pop line, then the whole things gets screwed up. What hints/tips do you have to get this fixed?

  6. #6
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Reading in a file in C++

    Quote Originally Posted by Amulon View Post
    So I have used your insights ...
    Whose insights? (Your own?)
    Quote Originally Posted by Amulon View Post
    And when it doesnt read any data (other than the string) on the pop line, then the whole things gets screwed up. What hints/tips do you have to get this fixed?
    "Screwed up", etc -- what do these mean??

    Keep in mind, that sensible people reading this post are not here to complete your assignment. Show us what you have thus far attempted.

  7. #7
    Join Date
    Jan 2013
    Beans
    17

    Re: Reading in a file in C++

    Uh... ? Why would I say that thanks for using my own insights? Of course I mean't the people who helped me.

    I am not mad/frustrated in the slightest about what people have replied. I am not saying that the people who helped me screwed up, I am saying that there is another problem I have discovered with my own code and I need some help with this.

  8. #8
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Reading in a file in C++

    Quote Originally Posted by Amulon View Post
    ... I have discovered with my own code and I need some help with this.
    It is the weekend; I do not have the luxury of depending on my crystal ball to get insight into the current state of your code.

    Perhaps if you posted it (that is, the code), along with the problem you are experiencing, I, or someone else, may be able to help you.

  9. #9
    Join Date
    Jan 2013
    Beans
    17

    Re: Reading in a file in C++

    Man, stop complaining.

  10. #10
    Join Date
    Jun 2007
    Location
    Maryland, US
    Beans
    6,288
    Distro
    Kubuntu

    Re: Reading in a file in C++

    Quote Originally Posted by Amulon View Post
    Man, stop complaining.
    You just sealed your fate.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

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