Stream I/O query

I'm trying to read an array of character strings (theList) to a text file and then read it back into another array of character strings (inList). I need inList to be NULL terminated (as is theList) i.e I want NULL as the final entry in inList.

I'm creating the file OK but I get no output from the read back. I'm new to streams so I'm probably doing something stupid. Any ideas on making this work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream.h>
#include <fstream.h>

char *theList[] = { "Item 1", "Item number 2", "Item name 3", "Item 4", NULL };
const int  numStrings = (sizeof(theList)/sizeof(theList[0]));
char *inList[numStrings];

int main()
{
  fstream theFile("textfile.txt", ios::in|ios::out);
  if (!theFile) {
    cerr << "Error opening file" << endl;
    return 1;
  }
  for (int i = 0; theList[i]; i++)
     theFile << theList[i] << ends;
  cout << "textfile.txt created" << endl;  <<==== OK to here

  for (int j = 0; j < numStrings; j++) {
    theFile.getline(inList[j], 80);
    cout << inList[j];
  }
  return 0;
}
You aren't really using a list or strings. So the read section isn't doing what you think.

The 'strings' in theList aren't strings at all, they're char arrays and they all can have different lenghths.

theList is an array of these variable length char arrays that are fixed in memory somewhere.

The list you have is good enough for writing to the file, but not good enough to read from the file. You need to use strings, and as you're reading an unspecified number of items from the file, a vector.

So your declaration of data structure looks like:
std::vector<std::string> anotherlist
kbw: I was a loose in my terminology - I should have said an 'array of pointers to char' rather than an 'array of character strings'.

However, I am using a pre-standard compiler (Turbo C++ - I know it's old but I'm using it!) so vectors are not available neither are 'strings'.

Is there a way to populate char *inList[numStrings]; with items read in from the file?

What I'm trying to do is: (a) hold a list of text items in a file and load it into my 'inList' at program startup. 'inList' may be subject to change during program running;

(b) at program end, save the - possibly revised - inList back out to a file.

I'm doing this so that I don't have to recompile the program every time I want to change the contents of 'inList'.
Try to use "theFile.flush();"

http://www.cplusplus.com/reference/iostream/ostream/flush/

It's effect is writting the data from buffers to the file.

1
2
3
4
5
6
7
8
9
for (int i = 0; theList[i]; i++)
     theFile << theList[i] << ends;
  cout << "textfile.txt created" << endl;  <<==== OK to here

  theFile.flush();

  for (int j = 0; j < numStrings; j++) {
    theFile.getline(inList[j], 80);
    cout << inList[j];



Or you should try to close and reopen your file between writting and reading.


Bye
I've solved my problem by redefining inList thus:

1
2
3
const int  maxString  = 20;
const int  numStrings = (sizeof(theList)/sizeof(theList[0]));
char inList[numStrings][maxString];


then

theFile.getline( inList[j], maxString);

populates inList correctly.
const int maxString = 20; theFile.getline(inList[j], 80);

You're reading a 80 character string into a 20 character buffer. You haven't solved the problem.
Sorry, typo - my code has theFile.getline(inList[j], 20);
Topic archived. No new replies allowed.