-
Reading a File
I opened and read a file in a program that I am working on. I have the following informantion in the file:
string intstring intstring intstring intstring intstring
int string intstring
As you can see, starting with the second string, the string starts right next to my previous int. I obviously need spacing there. However, my problem here is that I only want to show three pairs of string and int in each row and I'm having trouble doing that. Any tips?
I have two 1-D arrays, one for string and one for int, if that is any help.
Thanks!
-
My tip is to ask your question in the form of a question. Are you having troubles reading in like this, or is this what you're writing out and you want to change how you write it out?
-
I'm having trouble writing it out. To write it out I have this:
Code:
cout << States [row] << AdmNum [row];
The code for opening and closing the file is this:
Code:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
const int ROW_MAX = 1; //I should have 50 rows, however, when I use 50 the file I read is all messed up
ifstream &GetString (ifstream&, string&, const int, const char = '\n');
int main()
{
string States [ROW_MAX];
int AdmNum [ROW_MAX];
int RowsUsed;
string InFile;
ifstream datafile;
cout << "Enter the name of the date file containing information\nabout some of the United States : ";
cin >> InFile;
cin.ignore (100, '\n');
cout << endl;
datafile.open(InFile.c_str());
if (!datafile)
cout << "\nThe file could not be opened for reading.\n";
else
{
RowsUsed = 0;
GetString (datafile, InFile, 15); //I have a function for this
while (!datafile.eof() && RowsUsed < ROW_MAX)
{
States [RowsUsed] = InFile;
for ( int row = 0; row < ROW_MAX; row++)
{
States [row] = InFile;
datafile.ignore (21, '\n');
datafile >> AdmNum [row];
datafile.ignore (2000, '\n');
cout << States [row] << AdmNum [row];
GetString (datafile, InFile, 15);
}
}
}
datafile.close();
-
So you need to write a space after the int. And then to only get three on a line, you need to write an endl every third time. You can either put three in a loop (not recommended, by me anyway), or use a counter.