Reading from a program without getline

I have to write a program to read from a tsv file and display the content of that file. I'm not sure where to start with this program. Please help, I'm lost.

Last edited on
Perhaps you should post a few lines of the text in the tsv file, and indicate what information is to be displayed .

To display the contents of the file verbatim: std::cout << std::ifstream( "tsv_file.txt" ).rdbuf() ;
20424297 1092 CSCI 13500 B 3
20424297 1092 CSCI 13600 A- 3.7
20424297 1092 FREN 10100 B 3
20424297 1096 FREN 10200 W -1
20424297 1099 CSCI 23500 NC -1
are the first 5 lines of the text. So far I've tried this


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

int main ()
{ifstream inStream;

 inStream.open("StudentData.tsv");
 if (inStream.fail())
  {
  	cout << "file failed to open \n"
        return (1);
  }
  
 while (inStream)
 {
  string ID;
  inStream >> ID;
  cout << "EmplID is" << ID; 
 }
 
return 0;
}


But it doesn't display anything and I'm not sure how to use that line you posted.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

int main()
{
    const std::string file_name = "StudentData.tsv" ;

    {
        // create a test file
        std::ofstream(file_name) << "20424297\t1092\tCSCI\t13500\tB\t3\n"
                                    "20424297\t1092\tCSCI\t13600\tA-\t3.7\n"
                                    "20424297\t1092\tFREN\t10100\tB\t3\n"
                                    "20424297\t1096\tFREN\t10200\tW\t-1\n"
                                    "20424297\t1099\tCSCI\t23500\tNC\t-1\n" ;
    }

    {
        // dump the contents on stdout
        std::cout << std::ifstream(file_name).rdbuf() ;
    }
    std::cout << "\n----------------\n\n" ;
    {
        // read field by field
        std::ifstream file(file_name) ;
        std::string line ;
        while( std::getline( file, line ) ) // for each line in the file
        {
            // http://en.cppreference.com/w/cpp/io/basic_istringstream
            std::istringstream stm(line) ;

            std::string id ;
            int fld2 ;
            std::string fld3 ;
            int fld4 ;
            std::string grade ;
            double number ;

            if( stm >> id >> fld2 >> fld3 >> fld4 >> grade >> number )
            {
                std::cout << id << " | " << fld2 << " | " << fld3 << " | " << fld4 << " | "
                          << std::setw(2) << std::left << grade << " | " << number << '\n' ;
            }
            else std::cerr << "badly formed line '" << line << "'\n" ;
        }
    }
}

http://coliru.stacked-crooked.com/a/e6466eceaefe43f2
Last edited on
Is there any way to do it without getline though because my prof said "Both files are tab delimited (hence the tsv extension - tab separated values). And the data is structured so that you do not, actually you SHOULDN’T, have to use getline for your project."
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>

int main()
{
    const std::string file_name = "StudentData.tsv" ;

    {
        // create a test file
        std::ofstream(file_name) << "20424297\t1092\tCSCI\t13500\tB\t3\n"
                                    "20424297\t1092\tCSCI\t13600\tA-\t3.7\n"
                                    "20424297\t1092\tFREN\t10100\tB\t3\n"
                                    "20424297\t1096\tFREN\t10200\tW\t-1\n"
                                    "20424297\t1099\tCSCI\t23500\tNC\t-1\n" ;
    }

    {
        // dump the contents on stdout
        std::cout << std::ifstream(file_name).rdbuf() ;
    }
    std::cout << "\n----------------\n\n" ;
    {
        // read field by field, assuming that the input is well-formed
        std::ifstream file(file_name) ;
        std::string id ;
        int fld2 ;
        std::string fld3 ;
        int fld4 ;
        std::string grade ;
        double number ;

        while( file >> id >> fld2 >> fld3 >> fld4 >> grade >> number )
        {
                std::cout << id << " | " << fld2 << " | " << fld3 << " | " << fld4 << " | "
                          << std::setw(2) << std::left << grade << " | " << number << '\n' ;
        }
    }
}

http://coliru.stacked-crooked.com/a/75b511ecdd9b916b
Looking at your code, I made mine like this


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

int main ()
{ifstream inStream;

 inStream.open("StudentData.tsv");
 if (inStream.fail())
  {
  	cout << "file failed to open \n";
  	return (1);
  }
  
 string id;
 int scode;
 string name;
 int code;
 string grade;
 double num;
 
 while (inStream >> id >> scode >> name >> code >> grade >> num)
 { 
  cout << id << "	" << scode << " " << name << "	" 
       << code << "	" << grade << "   " << num << endl; 
 }
return 0;
}


However, it doesn't say the file failed to open and it doesn't display anything else either.
What does this print out?
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file( "StudentData.tsv" ) ;

    if( file.is_open() ) std::cout << "opened the file successfully\n" ;
    else std::cerr << "*** error: failed to open the file\n" ;
}
That prints out "opened the file successfully"
What does this print out?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream file( "StudentData.tsv" ) ;

    if( file.is_open() )
    {
        std::cout << "opened the file successfully\n\n" ;

        std::string id;
        std::string scode; // note: all are of type std::string
        std::string name;
        std::string code;
        std::string grade;
        std::string num;

        int num_recs = 0 ;
        while( file >> id >> scode >> name >> code >> grade >> num )
        {
            ++num_recs ;
            if( num_recs < 6 )
            {
                std::cout << id << "	" << scode << " " << name << "	"
                          << code << "	" << grade << "   " << num << '\n' ;
            }
        }

        std::cout << "\nin all " << num_recs << " records were read\n" ;
    }

    else std::cerr << "*** error: failed to open the file\n" ;
}
Oh that works and it prints out this
https://gyazo.com/2e0998f5dbf896847fc923d2f45f1bf1
Now try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream file( "StudentData.tsv" ) ;

    if( file.is_open() )
    {
        std::cout << "opened the file successfully\n\n" ;

        std::string id;
        /*std::string*/ int scode; // note: types restored to the original
        std::string name;
        /*std::string*/ int code;
        std::string grade;
        /*std::string*/ double num;

        int num_recs = 0 ;
        while( file >> id >> scode >> name >> code >> grade >> num )
        {
            ++num_recs ;
            if( num_recs < 6 )
            {
                std::cout << id << "	" << scode << " " << name << "	"
                          << code << "	" << grade << "   " << num << '\n' ;
            }
        }

        std::cout << "\nin all " << num_recs << " records were read\n" ;
    }
}
That displays
https://gyazo.com/66b261f16092b260c4cbd1b8ef99b85a
how did the number of records change between the 2 programs though?

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

int main ()
{ifstream inStream;

 inStream.open("StudentData.tsv");
   if (inStream.fail())
    {
     	cout << "file failed to open \n";
    	return (1);
    }
 
 
 if(inStream.is_open()) 
 {cout << "file opened successfully";
  string id;
  int scode;
  string name;
  int code;
  string grade;
  double num;
 
   int num_recs = 0;
   while (inStream >> id >> scode >> name >> code >> grade >> num)
     { 
        ++num_recs;
        if (num_recs < 6)
           {
	          cout << id << "	" << scode << " " << name << "	" 
                         << code << "	" << grade << "   " << num << endl; 
           
           }
     }
  }
 return 0;
}


I changed my program to but it only displays "file has opened successfully". I'm still not sure why
Last edited on
> how did the number of records change between the 2 programs though?

Either a. scode and code are not all integers and/or num are not all numbers.
or b. there are some badly formed lines in the file.

If the assertion that "the data is structured so that you do not have to use getline for your project." is merely debatable, shouting "actually you SHOULDN’T" is unquestionably asinine.

Ideally, read the file line by line with error-handling.

Or if a 'career teacher' would penalise you for that, read everything in as strings (and then convert if required with std::stoi, std::stod.)
Topic archived. No new replies allowed.