Hey all, I am trying to first write an array to a text text file, delete the array, then later in the program I need to use the array again so I need to open the text file and read the array and call it the same name as before. What I have so far is:

// to write array "truck_data2" to text file

fstream myfile;
  myfile.open ("truck_data2.txt");
for(int i=0;i<=21;i++)
    {
        myfile<<truck_data2[i]<<endl;
    }
  myfile.close();
delete[] truck_data2;

// and then later in program to read array from text file andname the array "truck_data2"

ifstream myfile;
  myfile.open("truck_data2.txt");

    while (! myfile.eof() )
    {
      getline (myfile,truck_data2);
    }
    myfile.close();

I think the writing the array to text file is ok but reading the array is wrong. Thanks in advence for any help.

Colm.

Recommended Answers

All 6 Replies

Code tags please and please post the code with the array declaration so we know what the type is (is it an array of characters?).

Regardless, you are overwriting everything you read in every trip through the loop.

while (! myfile.eof() )
{
getline (myfile,truck_data2);
}

Code tags please and please post the code with the array declaration so we know what the type is (is it an array of characters?).

Regardless, you are overwriting everything you read in every trip through the loop.

while (! myfile.eof() )
{
getline (myfile,truck_data2);
}

They're right. You're trying to store it in truck_data2. Well, that's simply a variable by this definition, you need to change it to something like:

int x = 0;
while (! myfile.eof() )
{ 
getline (myfile, truck_data2[x++]);
}

Hey, thanks for the reply and sorry about not using the code tags, I thought I just wrapped my code in [] which I did do, see above. Anyway I am getting an error messsage on the line where I have:

ifstream myfile;

which is line (3694) in my program

the error is:

C:\Users\colm\Desktop\C++\L.E.Test\L.E.Test.cpp(3694) : error C2371: 'myfile' : redefinition; different basic types
        C:\Users\colm\Desktop\C++\L.E.Test\L.E.Test.cpp(3670) : see declaration of 'myfile'

line (3670) is line:

ofstream myfile;

I am trying to overwrite the text file in every loop. I am generating new trucks in each run so I want to:

  1. save truck_data2 to text file
  2. do calculations without truck_data2
  3. read truck_data2 files from text file
  4. do calculations with truck_data2

In every loop I will have a new truck_data2 so I need to overwrite the text file in each loop. A typical truck_data2 array is:

double truck_data2[22] = {2, 23.692, 5.83, 60.567, 40.576, 0, 0, 0, 0, 0, 0, 0, 0, 4.678, 0, 0, 0, 0, 0, 0, 0, 0} 

Thanks again for your help.
Colm.

The code tags are [[B]CODE=c++[/B]][[B]/CODE[/B]].
You are getting the error because you are redefining myfile at two places: first as ifstream and then as ofstream which is illegal.
use fstream instead as:

fstream myfile;
myfile.open(filename);

As I can see, your array is of type double. getline() works only for strings and not for numerical data types. You should use the << and >> overloaded operators
Do not use myfile.eof() as it does not work as you suppose it to be. It will cause the loop to be iterating one additional time in the last.
Rather Use:

int x = 0;
while (myfile >>truck_data2[x++]));

Simple and best

Great, thanks sidd, I got it working it your advice, much appreciated.

Colm

Hey all, I am trying to first write an array to a text text file, delete the array, then later in the program I need to use the array again so I need to open the text file and read the array and call it the same name as before. What I have so far is:

// to write array "truck_data2" to text file

fstream myfile;
  myfile.open ("truck_data2.txt");
for(int i=0;i<=21;i++)
    {
        myfile<<truck_data2[i]<<endl;
    }
  myfile.close();
delete[] truck_data2;

// and then later in program to read array from text file andname the array "truck_data2"

ifstream myfile;
  myfile.open("truck_data2.txt");

    while (! myfile.eof() )
    {
      getline (myfile,truck_data2);
    }
    myfile.close();

I think the writing the array to text file is ok but reading the array is wrong. Thanks in advence for any help.

Colm.

getline gets a whole line, why not separate the inpout items items with a space (whitespace) and use <<

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.