How to read files

This is a discussion on How to read files within the C++ Programming forums, part of the General Programming Boards category; I want to read a bunch of numbers from a file. I have declared a few structures in structures I ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    15

    How to read files

    I want to read a bunch of numbers from a file.

    I have declared a few structures in structures

    I passed a pointer from main to a local function and declared a dunamic array in it.

    int readData(Point base[2], Vertex* vertices[])
    {
    int nPts = 0;
    Vertex *vertex = new Vertex[48];
    Vertex *pVertex = &vertex[0]; <----should I be using this pointer to read the values?
    ......
    cout<<vertex[0].bldgId;
    fin>>Vertex[0].bldgId;
    cout<<vertex[0].bldgId;
    }

    I am trying to figure out how to read 1 of the values bldgId to the
    dynamic array. The value in the file is '1' but it reads '4475424' for both cout statements.

    My guess is its not reading it correctly? I think I declared and opened the file correctly. How could I read in the value?
    Thx

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Assuming your file is a text file (readable with an editor) what you have should work (no need for a pointer).

    It's possible the file never got opened (maybe check to be sure):
    Code:
    fin.open(filename, ios::in);
    if (!fin)
    {
       cout "Error opening file:" << filename << endl;
       return;
    }

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    Ok I put these lines before the fin part but it still doesnt work.


    ifstream fin("survey.dat");

    if (!fin)
    {
    cout<<"Error opening file:"<<"survey.dat"<<endl;
    exit(1);
    }
    fin.open("survey.dat", ios::in); <--- if I place this where u said it give the error message so I put it here.

    Why wont the file open?!

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >ifstream fin("survey.dat");

    This line actually opens the file, so you don't need the fin.open(). Use one or the other.

    If you are getting the error message "Error opening file", then it probably is looking in the wrong directory, so try putting the full path. For example, on a windows computer, something like:

    ifstream fin("\\dir\\subdir\\survey.dat");

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    Ok cool that worked but I forgot I actually need to read them into the pointer vertices[]

    Code:
    	while (! fin.eof())
    	{
    		fin>>vertices[nPts]->bldgId;
    		nPts++;
    	}
    It keeps giving me "Unhandled Exception" error! I'm sure thats how to read pointers? Am I missing something?

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    If you actually need to pass this newly allocated array back to your main() function, then you will need a double pointer. Your code will look similar to this.
    Code:
    int readData(Point base[2], Vertex** vertices);
    int main(void)
    {
       Point base[2];
       Vertex *vertices;
       int i;
    
       readData(base,&vertices);
       for (i=0; i<48; i++)
          cout << vertices[i].bldgId << endl;
    
       //Clean up memory when done
       delete []vertices;
       return 0;
    }
    
    int readData(Point base[2], Vertex** vertices) 
    { 
       int nPts = 0; 
       int i;
    
       *vertices = new Vertex [48]; 
    
       ifstream fin("survey.dat");
       if (!fin)
       {
          cout << "Could not find file.\n";
          return 0;
       }
       while (fin>>(*vertices)[nPts].bldgId)
       {
          cout<<(*vertices)[nPts].bldgId << endl; 
          nPts++;
       }
       cout << "nPts:" << nPts << endl;
       return 1;
    }
    You can use a single pointer (Vertex *vertices) if you allocate the array in the main() function.
    Also be sure that nPts is 48 at the end of the function, otherwise you may be going past the end of the array.
    If nPts is your return value, then change the last line above to:
    return nPts;

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    15
    Thx swoopy. I don't think I need double pointers. Don't even know what that is!

    I figured it out though. Just needed to dynamically declare vertices in the loop.

    And now I am trying to do a bubble sort of the data read in but it's giving me grabage values. w/o the bolded lines of code, the output is correct (unsorted of course) so is it not possible to swap structures like that?

    Code:
    // Sort the vertices according to the angle of the relative polar coordinates
    void sortVertices(Vertex* vertices[], int nPts)
    {
          Vertex temp;
          bool exchange;
          
          do
          {
                exchange = false;
                for (int counter=0; counter < nPts; counter++)
    	  if (vertices[counter]->polar.theta > vertices[counter+1]->polar.theta)
      	 {
    	      vertices[counter] = new Vertex;
    	 
                          temp = *vertices[counter];
    	      *vertices[counter] = *vertices[counter+1];
    	      *vertices[counter+1] = temp;
    				
    	      exchange = true;		
    
    	      cout<<vertices[counter]->polar.d<<" "<<vertices[counter]->polar.theta<<" "<<endl;
    	 }
    	 nPts--;
          }
          while (exchange == true);
    }

  8. #8
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I'm glad you got the allocation figured out. For the swap, normally you would have to swap each individual part of the structure. However in your case I believe all you need to do is swap the pointers, so this should work.
    Code:
    //Declare temp as:
    
          Vertex *temp;
    
    //Change the swap statements within the loop to this:
    	      temp = vertices[counter];
    	      vertices[counter] = vertices[counter+1];
    	      vertices[counter+1] = temp;
    And leave this statement out:
    vertices[counter] = new Vertex;

  9. #9
    Registered User
    Join Date
    Jan 2002
    Posts
    106
    ofstream fin("filename.txt");
    if (fin.fail()) {
    cout << "\nCouldn't find the file" << endl; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read Only files
    By DominicTrix in forum C++ Programming
    Replies: 4
    Last Post: 12-11-2004, 01:55 AM
  2. using threads to write & read from files
    By kishorepalle in forum C Programming
    Replies: 4
    Last Post: 10-19-2004, 05:19 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. trying to read all asci char's in files
    By brane_sail in forum C++ Programming
    Replies: 1
    Last Post: 09-02-2002, 11:33 AM
  5. Replies: 1
    Last Post: 07-24-2002, 06:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21