Reading File Output HELP

The code below reads my file that has 5 names. How would I get the output to show all the names and tell me "The first person is..." and "The last person is..." How would I make the output say that?






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

int main() {
	string line;
	ifstream myfile("student.txt");
	if (myfile.is_open())
	{
		while (getline(myfile, line))
		{
			cout << line << '\n';
		}
		myfile.close();
	}

	else cout << "Unable to open file";

	return 0;
} 
Last edited on
Hello LyonPredator,

Using what you have:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool first{ true };

std::getline(myfile, line);

while (1)
{
     if (!myfile)
    {
        std::cout << "The last person is: " << line << std::endl;
        break;
    }

    if (first)
    {
        first = false;
        std::cout << "The first person is: " << line << std::endl;
    }

    else
        std::cout << line << std::endl;
    }

    std::getline(myfile, line);
}


Not 100% sure about this, but you can try it while I test it.

Hope that helps,

Andy
Hello LyonPredator,

I did not think that would work, but this does:

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
int main()
{
	std::string line;
	std::ifstream myfile("student.txt");
	bool first{ true };
	size_t count{ 1 };

	if (myfile.is_open())
	{
		
		while (std::getline(myfile, line))
			count++;

		myfile.clear();
		myfile.seekg(0);

		for (size_t lc = 1; lc < count; lc++)
		{
			std::getline(myfile, line);

			if (lc == count - 1)
			{
				std::cout << "The last person is: " << line << std::endl;
				break;
			}

			if (first)
			{
				first = false;
				std::cout << "The first person is: " << line << std::endl;
			}

			else
				std::cout << line << std::endl;

		}

	}

	else
		std::cout << "Unable to open file";

		myfile.close();
}


If I understand correctly I think this is what you want. If not let me know.

Hope that helps,

Andy
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 <fstream>
#include <iostream>
#include <string>

int main()
{
    std::ifstream myfile("student.txt");
    if(!myfile) {
        std::cout << "Can't open input file.\nExiting now.\n";
        return 0;
    }

    std::string ... // you need to store the value for first person,
                    // last person and the line you read at each iteration

    while(std::getline(myfile, /* store inside the std::string for the line */))
    {
        if( ??? )   // check if you've already saved something in the 
        {           // std::string for the first person name
            // if not, save the first person name from the line
        }

        if(!line.empty()) { // if this is *NOT* the last loop iteration
            // save the last person name
        }
        std::cout << /* std::string for the line */ << '\n';
    }

    std::cout << "The first person is " << // std::string for the first person
              << "\nThe last person is " << // std::string for the last person
              << '\n';
    return 0;
}

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

int main()
{
   string first, last, line;

   ifstream myfile( "student.txt");
   if ( myfile.is_open() )
   {
      getline( myfile, first );
      while ( getline( myfile, line ) ) last = line;
      myfile.close();

      cout << "The first name is " << first << '\n';
      cout << "The last name is " << last  << '\n';
   }
   else
   {
       cout << "Unable to open file";
   }
} 

Topic archived. No new replies allowed.