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...
:(

>>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;
}

Here is the file...

Attachments
Maine           1124660  33215 1820 23 Augusta        ME 05000
New Hampshire    920610   9304 1788  9 Concord        NH 03900
Vermont          511456   9609 1791 14 Montpelier     VT 06000
Massachusetts   5767037   8257 1788  6 Boston         MA 02800
Rhode Island     947154   1214 1790 13 Providence     RI 03000
Connecticut     3107576   5009 1788  5 Hartford       CT 07000
New York       17557288  49576 1788 11 Albany         NY 15000
Pennsylvania   11866728  45333 1787  2 Harrisburg     PA 19700
Delaware         595225   2057 1787  1 Dover          DE 20000
Virginia        5346279  40815 1788 10 Richmond       VA 24700
North Carolina  5874429  52712 1789 12 Raleigh        NC 29000
South Carolina  3119208  31055 1788  8 Columbia       SC 30000
Georgia         5464265  58876 1788  4 Atlanta        GA 32000
Michigan        9258344  58216 1837 26 Lansing        MI 50000
Ohio           10797419  41222 1803 17 Columbus       OH 46000
Indiana         5490179  36291 1816 19 Indianapolis   IN 48000
Wisconsin       4705335  56154 1848 30 Madison        WI 55000
Kentucky        3661433  40395 1792 15 Frankfort      KY 43000
Tennessee       4590750  42244 1796 16 Nashville      TN 38600
Mississippi     2520638  47716 1817 20 Jackson        MS 40000
Louisiana       4203972  48523 1812 18 Baton Rouge    LA 71600
Arkansas        2285513  53104 1836 25 Little Rock    AR 73000
Minnesota       4077148  84068 1858 32 Saint Paul     MN 57000
Montana          786690 147138 1889 41 Helena         MT 60000
North Dakota     652695  70665 1889 40 Bismark        ND 59000
South Dakota     690178  77047 1889 39 Pierre         SD 58000
Wyoming          470816  97914 1890 44 Cheyenne       WY 83200
Colorado        2888834  10247 1876 38 Denver         CO 82000
Kansas          2362208  82264 1861 34 Topeka         KS 70000
Oklahoma        3025266  69919 1907 46 Oklahoma City  OK 68000
Texas          14228383 267339 1845 28 Austin         TX 75000
New Mexico      1299968 121666 1912 47 Santa Fe       NM 80000
Arizona         2717866 113909 1912 48 Phoenix        AZ 89000
Utah            1461037  84916 1896 45 Salt Lake City UT 85000
Nevada           799184 110540 1864 36 Carson City    NV 90000
Idaho            943935  83557 1890 43 Boise          ID 64000
West Virginia   1949644  24181 1863 35 Charleston     WV 27000
Washington      4130163  68192 1889 42 Olympia        WA 99500
Oregon          2632663  96981 1859 33 Salem          OR 98000
California     23668562 158693 1850 31 Sacramento     CA 96700
Alaska           400481 586400 1959 49 Juneau         AK 99999
New Jersey      7364518   7836 1787 03 Trenton        NJ 09000
Hawaii           965000   6424 1959 50 Honolulu       HI 96900

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.

This article has been dead for over six months. Start a new discussion instead.