Hi everyone,

I have a 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 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!

post the first two lines of the actual file

100 101something 102another 103yetanother

something like above?


Here is an example of how you might do it. This technique should work with an input stream instread of using stringstream in my example

#include <string>
#include <sstream>
#include <iostream>
using namespace std;


int main()
{
    int i;
    int arry[4] = {0};
    string strarray[3];
    string str = "100 101something 102another 103yetanother";
    stringstream ss(str);
    ss >> arry[0];
    for(i = 0; i < 3; i++)
    {
        ss >>arry[i+1] >> strarray[i];
    }
    
    for(i = 0; i < 3; i++)
    {
        cout << arry[i+1] << " " << strarray[i] << "\n";
            
    }
}

Yes, the file look slike that, minus the first int (100).
So far I have this code:

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>


using namespace std;


const int ROW_MAX = 1;


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]; //what im writing out. I need only three strings (states) and three ints (AdmNum) per row. I'm supposed to have 50 rows (one for each state), however, when I use 50 rows as my max the file is not read correctly for some reason and I get a bunch of nothing. 
				GetString (datafile, InFile, 15);
			}
		}
	}
	datafile.close();
	return 0;
}

I think you are just overcomplicating that entire process. The solution is a lot simpler than what you have posted. First, toss out that GetString() function because its not necessary. This is all there is to it.

string line;
int counter = 0;
while( std::getline(datafile, line) )
{
      std::stringstream str(line);
      str >> AdmNum[counter] >> states[counter];
      counter++;
}

It would be better is you created a structure with the int and string, then made a vector of structures. That makes it a lot easier to keep the int and strings together, especially if you want to sort the arrays.

Are you given the file that looks like this:

intstring intstring intstring

or are you creating that file? If that's what you are given then read an entire line into a stringstream and parse it using a technique similar to what Ancient Dragon previously posted.

You need a different value than 1 for ROW_MAX.

Don't use the return value of eof() to control your loop. It will be your downfall sooner or later if you do. Instead do something like this:

string temp;
while(getline(datafile, temp)) //get a new line
{
   stringstream ss(temp); //put new line in ss
   //parse temp here using SS
   //display parsed information however you want
}

EDIT: I need to learn how to type or how to think faser!

I think you are just overcomplicating that entire process. The solution is a lot simpler than what you have posted. First, toss out that GetString() function because its not necessary. This is all there is to it.

string line;
int counter = 0;
while( std::getline(datafile, line) )
{
      std::stringstream str(line);
      str >> AdmNum[counter] >> states[counter];
      counter++;
}

It would be better is you created a structure with the int and string, then made a vector of structures. That makes it a lot easier to keep the int and strings together, especially if you want to sort the arrays.

I'm required to use the GetString function for this program. (Professor wants it this way).
I thought of using a structer, that way I can have string and int members but we are not using structures until the next program. I don't know about vectors yet either...
:(

EDIT: I need to learn how to type or how to think faser!

All great minds run in the same channel :)

>>I'm required to use the GetString function for this program. (Professor wants it this way).
Ok, so what does that function do ?

The function reads up to15 characters from my text file. After the 15 characters are encountered, according to the code I've written, the 21 next characters in the text file are skipped and then my program reads the int array.

GetString function looks like this:

ifstream &GetString (ifstream& datafile, string& InFile, const int maxlen, const char stopper)
{
	char temp;
	InFile = "";
	while ((int)InFile.length () < maxlen && datafile.peek() != stopper)
	{
		datafile.get (temp);
		InFile = InFile + temp;
	}
	return datafile;
}

post the data file -- zip it up and attach it.

Here is the file...

The file is set up with fields, each field having the same width (number of characters) for each line with the information for a given state all on one line. The fields are padded either on the left or the right with spaces to be sure that each field has exactly the same number of characters per field.

The name of the state is the first field on each line. The first field is 15 char long. Therefore it can be extracted using your own GetString() function. Fields 2-5 a are all numerical. The 6 field appears to be the name of State Capital and may have embedded spaces, but again is of a fixed length, and again is 15 char long. The seventh field is always two char long and the last field is again numerical.

To extract the information from this file I would extract field one using GetString (or whatever) by reading 15 char from the file and place them in a char array declared to be 15 char long. I would remove the padding, if any, and then I'd null terminate the character array to create a string. Then I'd extract the next 4 fields using the >> operator. The sixth field I'd use the GetString() method again. The last two fields I'd use >> again.

Once I had extracted all the information for a given line I'd either store the information for later use or do something with it right away. I'm still not sure what you are trying to do with the information once you have it, so I can't say what I'd do at this time.