Hi guys just trying to understand Getline and implement it in a problem im trying to solve.

First off i have a program that is working:

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;


void main()
{

	ifstream fin("data.txt");
	ofstream fout;

	char name[30];
	char jersey_number[10];
	char best_time[10];
	char sport[40];
	char high_school[40];

	while(!fin.getline(name, 30, '|').eof())
	{
		fin.getline(jersey_number, 10, '|'); 
		fin.getline(best_time, 10);         
		fin.getline(sport, 40, '|');         
		fin.getline(high_school, 40);    
                
	        cout << jersey_number << best_time << sport << high_school << endl;
        }

	
}

This works fine with the input data text file containing the following:

John|83|52.2
swimming|Jefferson
Jane|26|10.09
sprinting|San Marin


Now the problem! I tried to learn from this and make a new program, which should do almost the same thing, read data from a text file, and cout the information i want.

The data in the text file is in a different format, for example:

firstName middleName surname

38 47 38 27 36

firstName middleName surname

84 37 29 34 72

Here is what i programmed, going from the first example:

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;


void main()
{

	ifstream fin("test.txt");
	ofstream fout("output.txt");

	char name[30];
	char fullName[30];
	char marks[30];

	while(!fin.getline(name, 30, '\n').eof())
	{
		fin.getline(fullName, 30, '\n'); 
		fin.getline(marks, 30, '\n');         
        
		cout << fullName << " " << marks << endl;
	}

	
}

All this does is bring up the program window which just runs forever, what am i doing wrong?

The different thing between the working program and the one that isnt working is that the working one uses "|" but 2nd one uses "\n" for end of each line i believe.

ideas anyone or help? :)

Ok so i got it working, the program now takes data from input file and puts it into an output file.

Only thing is i dont want to output all the numbers, i want them in an array, how do i put them into an array do i have to do something different than getline?

Here is what i have:

#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;

bool getInputFilename(char fname[])						
{
	ifstream fin;										

	cout << "Please enter the filename for input : ";
	cin >> fname;										
														
	fin.open(fname, ios::nocreate);						
														
	if (!fin.is_open())
		return false;									 
	
	fin.close();
	return true;										
}


bool getOutputFilename(char fname[])					
{
	ofstream fout;

	cout << "Please enter the filename for output : ";
	cin >> fname;										
														
	fout.open(fname);									

	if (fout.fail())									
		return false;

	fout.close();
	return true;										
}


int main()
{
	ifstream fin;
	ofstream fout;

	string fullName;
	string marks;


	char IFname[20], OFname[20];


	while (!getInputFilename(IFname))
	{
		cout << "Invalid filename try again!\n\n";
	}

	while (!getOutputFilename(OFname))
	{
		cout << "Invalid filename try again!\n\n";
	}


	fout.open(OFname);
	fin.open(IFname);
	
	while(getline(fin, fullName) && getline(fin, marks))
	{
		fout << fullName << " " << marks << endl;
	}


	fout.close();
	fin.close();
}

Any thoughts / ideas / help appreciated.

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