Using getline to read file input and seperate string contents appropriately

Feb 5, 2017 at 2:17pm
Good morning, everyone. As part of an assignment, I am tasked with using the header file <cstring> and the cin member function, getline, to extract file input and store it in character arrays (c-strings). The assignment prohibits the use of sstream, the header file <string> and the use of extraction operators (>>) to read from the input file. A single line from the input file may look like the following:

Mario Delagarza 437734 78 89 96 85 77 98

Unlike the line above, the line from the input file may contain variable amounts of whitespace between its contents. How can the input be read using getline and then each piece of information be manipulated so as to ignore the variable amounts of whitespace between those contents that are seperated by more than one whitespace?
Last edited on Feb 5, 2017 at 2:19pm
Feb 6, 2017 at 1:31am
Just read the string and remove the spaces.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
using namespace std;

int main(int argc, char** argv) {

// Read from file
  string line;
  line="Mario Delagarza 437734 78 89 96 85 77 98";

  string output;  
  for (int i=0; i < line.size(); i++)
  {
  	if (line[i] == ' ') // if space is found 
	  	{
			cout << output << endl;
			output = ""; 
		}
	else // if no space is found
		{output=output+line[i];}
  }
return 0;
}
Feb 6, 2017 at 1:42am
Thank you for your time. Unfortunately, the assignment asks that we use getline so we have to manipulate the c-string, or character array instead of a string. I've got the following so far and at the moment the latter is not even doing anything. I don't receive an error or any value returned. The console window opens and just sits there until I exit out of it using the exit button. The former is closer but for whatever reason, it isn't writing to an output file on my computer while on a friend's it does:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;

int main( )
{
const int CLASS_SIZE = 40,
RECORD_SIZE = 256,
FIRST_NAME = 11,
LAST_NAME = 13,
ID_NUMBER = 7,
TEST_GRADES = 6,
TEST_SIZE = 3;

char student_record[CLASS_SIZE],
first_name[FIRST_NAME],
last_name[LAST_NAME],
id_number[ID_NUMBER],
test_one[TEST_SIZE],
test_two[TEST_SIZE],
test_three[TEST_SIZE],
test_four[TEST_SIZE],
test_five[TEST_SIZE],
test_six[TEST_SIZE],
space[RECORD_SIZE];

int test_grades[TEST_GRADES],
counter = 0;

ifstream fin;
fin.open("student_input.dat");

if ( !fin )
{
cout << "Input file failed to open. ***Program Terminating***";

return -1;
}

ofstream fout;
fout.open("student_results.dat");

if ( !fout )
{
cout << "Output file failed to open. ***Program Terminating***";

return -2;
}

for ( int records = 0; records < CLASS_SIZE; records++ )
{
fin.getline( student_record, RECORD_SIZE, '\n' );

if ( fin.eof() )
break;

while ( student_record[counter] != ' ' )
{
first_name[counter] = student_record[counter];

cout << first_name[counter];
//fout << first_name[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( student_record[counter] != ' ' )
{
last_name[counter] = student_record[counter];

cout << last_name[counter];
//fout << last_name[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( student_record[counter] != ' ' )
{
id_number[counter] = student_record[counter];

cout << id_number[counter];
//fout << id_number[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( student_record[counter] != ' ' )
{
test_one[counter] = student_record[counter];

cout << test_one[counter];
//fout << test_one[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( student_record[counter] != ' ' )
{
test_two[counter] = student_record[counter];

cout << test_two[counter];
//fout << test_two[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( student_record[counter] != ' ' )
{
test_three[counter] = student_record[counter];

cout << test_three[counter];
//fout << test_three[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( student_record[counter] != ' ' )
{
test_four[counter] = student_record[counter];

cout << test_four[counter];
//fout << test_four[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( student_record[counter] != ' ' )
{
test_five[counter] = student_record[counter];

cout << test_five[counter];
//fout << test_five[counter];

counter++;
}
//fout << " ";
cout << " ";
while( student_record[counter] == ' ' )
{
space[counter] = student_record[counter];

counter++;
}
while( isalnum(student_record[counter]) )
{
test_six[counter] = student_record[counter];

cout << test_six[counter];
//fout << test_six[counter];

counter++;
}

counter = 0;

//fout << endl;

cout << endl;
}

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

return 0;
}



#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>

using namespace std;

int main( )
{
const int CLASS_SIZE = 40,
RECORD_SIZE = 256,
FIRST_NAME = 11,
LAST_NAME = 13,
ID_NUMBER = 7,
TEST_GRADES = 6,
TEST_SIZE = 3;

char student_record[CLASS_SIZE],
first_name[FIRST_NAME],
last_name[LAST_NAME],
id_number[ID_NUMBER],
test_one[TEST_SIZE],
test_two[TEST_SIZE],
test_three[TEST_SIZE],
test_four[TEST_SIZE],
test_five[TEST_SIZE],
test_six[TEST_SIZE],
space[RECORD_SIZE];

int test_grades[TEST_GRADES],
counter = 0,
space_counter = 0;

ifstream fin;
fin.open("student_input.dat");

if ( !fin )
{
cout << "Input file failed to open. ***Program Terminating***";

return -1;
}

ofstream fout;
fout.open("student_results.dat");

if ( !fout )
{
cout << "Output file failed to open. ***Program Terminating***";

return -2;
}

for ( int records = 0; records < CLASS_SIZE; records++ )
{
fin.getline( student_record, RECORD_SIZE, '\n' );

if( fin.eof() )
break;

while ( student_record[counter] != '\n' )
{
if ( isalnum(student_record[counter]) )
{
fout << student_record[counter];

counter++;

space_counter = 0;
}
else if ( isspace(student_record[counter]) && space_counter == 0 )
{
fout << student_record[counter];

space_counter++;

counter++;
}
}

fout << endl;

counter = 0;

space_counter = 0;
}

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

return 0;
}

Feb 6, 2017 at 9:41am
Why don't you use sscanf ? http://www.cplusplus.com/reference/cstdio/sscanf/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
  char input[] = "Mario Delagarza   437734   78 89    96 85  77    98";
  char first_name[32] = {0}, last_name[32] = {0};
  int scores[7] = {0};

  sscanf(input, "%s %s %d %d %d %d %d %d %d", first_name, last_name, &scores[0],
    &scores[1], &scores[2], &scores[3], &scores[4], &scores[5], &scores[6]);
  std::cout << "Result: \n";
  std::cout << "First name: " << first_name << "\n";
  std::cout << "Last name: " << last_name << "\n";
  std::cout << "Scores: \n";
  for (int i : scores)
    std::cout << i << "\t";
  std::cout << "\n\n";
  system("pause");
  return 0;
}

Feb 6, 2017 at 1:03pm
Good morning, Thomas1965. I am unfamiliar with sscanf but if I did know how to use it, it wouldn't matter for this assignment because we are supposed to use getline to get each record from an input file and then manipulate the c-string, meaning that each character would be part of a single character array including the numbers and from that c-string, I would need to sort through each element and determine whether it is part of the first name, last name, id number, and each individual grade. The records are not hard-coded into any variables or entered on the console. They are to be retrieved from an input file. I apologize if I didn't clarify that in the beginning sufficiently.
Feb 6, 2017 at 2:01pm
OP: I'm sorry but your assignment is just ridiculous and poorly thought out. Did your instructor explain what s/he wants you to learn out of these absurd set of restrictions?
Feb 6, 2017 at 2:26pm
Original post, quote:
I am tasked with using the header file <cstring>

Well, in that case, you could use the function strtok from that header.
http://www.cplusplus.com/reference/cstring/strtok/

You might also use some of the other functions such as strcpy, depending on requirements.
http://www.cplusplus.com/reference/cstring/strcpy/


Also, I'm not sure whether you need to convert the numeric data from string to integer form? If so there are functions for that. Doing the conversion yourself is possible too, but would add complexity to an already messy set of requirements.

Feb 6, 2017 at 2:26pm
Good morning, gunnerfunner. I understand it is frustrating. I have tried about three different algorithms and there is some issue with all of them. I think my instructor wants us to learn the most basic functions and how they are used prior to using more advanced techniques so we know how to build on the older and more basic functions or eventually learn why objects work the way they do.
Feb 6, 2017 at 2:38pm
Well good luck with that but it seems it's your instructor who doesn't want to learn new stuff!
Topic archived. No new replies allowed.