Results 1 to 3 of 3

Thread: Reading lines from a file.

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Posts
    19

    Reading lines from a file.

    Hi,

    I wrote a function where I want to read 2 lines from a file. Each line will be soimething like

    A + B - C
    A - B + A - D - F

    etc

    I wrote a function as follows:

    Code:
    void GetData(string& Infix, string& Pre)
    {
      ifstream ins;
      ins.open("data.txt");
      char buffer[80];
      
      int Count = 0; // Counter used to update position in array where we are saving the floppies contents to.
      
      string data;
      string FloppyData[2];
      
      while(!ins.eof()){
        getline(ins,data);
        FloppyData[Count] = data;
        Count++;
      }
      
      Infix = FloppyData[0];
      Pre = FloppyData[1];
      
    }
    I can't see whats wrong with it. I want the FloppyData array to hold two strings, then assign Infix and Pre to the right values in the array.

    Thanks in advance for any feedback.

  2. #2
    Junior Member
    Join Date
    Nov 2002
    Location
    Left past the postbox
    Posts
    30

    Reply

    All I did to get your code working is specified an absolute path to the file



    Code:
    #include "iostream"
    #include "string"
    #include "fstream"
    
    
    using namespace std;
    
    void GetData(string& Infix, string& Pre)
    {
    
      ifstream ins("c:\\data.txt");
     
      
      int Count = 0; // Counter used to update position in array where we are saving the floppies contents to.
      
      string data;
      string FloppyData[2];
      
      while(!ins.eof()){
        getline(ins,data);
        FloppyData[Count] = data;
        Count++;
      }
      
      Infix = FloppyData[0];
      Pre = FloppyData[1];
      
    }
    
    
    int main()
    {
    	string i,p;
    
    	GetData(i,p);
    
    	cout << i << endl;
    	cout << p;
    
    	system("Pause");
    
    	return 0;
    
    }

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2002
    Posts
    19
    I can compile that, but when I try to run it comes up with a pop up message saying - btw, I wouldn't have a clue where the created error log is.

    Program Error

    file.exe has generated errors and will be closed by Windows.

    An error log is being created.

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