Reading a File

This is a discussion on Reading a File within the C++ Programming forums, part of the General Programming Boards category; I opened and read a file in a program that I am working on. I have the following informantion in ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10

    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!

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  3. #3
    Registered User
    Join Date
    Aug 2008
    Location
    California
    Posts
    10
    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();

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 01:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21