Jump to content

Read all the lines C++

Go to solution Solved by CookieMaster,

Figured it out 

{
		ifstream File((filename + type).c_str()); // Load file and file type stated above.
		while (getline(File, img))
		cout << img << endl; // Display the image.
		system("pause"); // Don't exit application
		//Todo: add array.
	}

 

26 minutes ago, TheKDub said:

I'm not familiar enough with C++ file handling to be able to help much there, though the basic idea of it would just be something like a while loop checking to see if there's another line in the file to be read, then reading that line within the loop.

Thanks again. While was just needed and then got rid of closing the file. 

Hello, so first off my code works. My only problem is that it reads the first line only. The file type is an .asc. My code is down bellow, what do I have to do to make it read the entire document?

 

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
	int response;
	string filename = "cookies"; // Change this if image file name is different.
	string type = ".asc"; // Change this if file type is different.
	string img; /

	cout << "Welcome to Tx Paint (Tunxis Paint)" << endl;
	cout << "[1] Load image" << endl;
	cout << "[2] Draw line in image" << endl;
	cout << "[3] Draw box in image" << endl;
	cout << "[4] Save image" << endl;
	cout << "[5] Exit" << endl;
	cin >> response; 

	if (response == 1)

	{
		ifstream File((filename + type).c_str()); // Load file and file type stated above.
		if (File.is_open())
		getline(File, img); 
		File.close();
		cout << img << endl; // Display the image.
		system("pause"); 
		//Todo: add array.
	}

    return 0;
}

 

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/
Share on other sites

Link to post
Share on other sites

I'm not too familiar with C++, though have used Java and VB.Net plenty, so I'll give it a go...

 

I'd guess it's because you're only getting a single line, and not running a loop checking for if there's a next line, then reading that line. (Or at least from what I can tell there isn't one)

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9588949
Share on other sites

Link to post
Share on other sites

1 minute ago, TheKDub said:

I'm not too familiar with C++, though have used Java and VB.Net plenty, so I'll give it a go...

 

I'd guess it's because you're only getting a single line, and not running a loop checking for if there's a next line, then reading that line. (Or at least from what I can tell there isn't one)

Yeah true. I guess my question is more of a how to, but I will keep looking and trying and seeing if I can figure it out. Thanks for the advice as well.

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9588960
Share on other sites

Link to post
Share on other sites

Just now, CookieMaster said:

Yeah true. I guess my question is more of a how to, but I will keep looking and trying and seeing if I can figure it out. Thanks for the advice as well.

I'm not familiar enough with C++ file handling to be able to help much there, though the basic idea of it would just be something like a while loop checking to see if there's another line in the file to be read, then reading that line within the loop.

Specs: CPU - Intel i7 8700K @ 5GHz | GPU - Gigabyte GTX 970 G1 Gaming | Motherboard - ASUS Strix Z370-G WIFI AC | RAM - XPG Gammix DDR4-3000MHz 32GB (2x16GB) | Main Drive - Samsung 850 Evo 500GB M.2 | Other Drives - 7TB/3 Drives | CPU Cooler - Corsair H100i Pro | Case - Fractal Design Define C Mini TG | Power Supply - EVGA G3 850W

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9588968
Share on other sites

Link to post
Share on other sites

Figured it out 

{
		ifstream File((filename + type).c_str()); // Load file and file type stated above.
		while (getline(File, img))
		cout << img << endl; // Display the image.
		system("pause"); // Don't exit application
		//Todo: add array.
	}

 

26 minutes ago, TheKDub said:

I'm not familiar enough with C++ file handling to be able to help much there, though the basic idea of it would just be something like a while loop checking to see if there's another line in the file to be read, then reading that line within the loop.

Thanks again. While was just needed and then got rid of closing the file. 

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9589043
Share on other sites

Link to post
Share on other sites

6 hours ago, CookieMaster said:

Figured it out 


{
		ifstream File((filename + type).c_str()); // Load file and file type stated above.
		while (getline(File, img))
		cout << img << endl; // Display the image.
		system("pause"); // Don't exit application
		//Todo: add array.
	}

 

Thanks again. While was just needed and then got rid of closing the file. 

Some finer points:

 

Since C++11, ifstream's contructor has an overload that takes a std::string as filename, so you no longer need "c_str":

		ifstream File(filename + type); 

Put braces around every conditional block, even if it contains only 1 line, otherwise you'll quickly start making errors when changing the code afterwards, it's also confusing to read without the braces/proper indentation. 

		while (getline(File, img))
		{
			cout << img << '\n';
		}

Also note the endl was changed to '\n'. std::endl is more then simply a newline, it also flushes the output buffer, which can be expensive when done in a loop like it is here, it kinda defeats the purpose of having a buffer in the first place.

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9589780
Share on other sites

Link to post
Share on other sites

As a small side note, don't ever use system("pause"); as it's not platform independent. Use std::cin's ignore facility to block the program and then discard whatever is entered.

And, if my thought-dreams could be seen,
they'd probably put my head in a guillotine.
But, it's alright, ma, it's life, and life only.

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9593117
Share on other sites

Link to post
Share on other sites

11 hours ago, Unimportant said:

Some finer points:

 

Since C++11, ifstream's contructor has an overload that takes a std::string as filename, so you no longer need "c_str":


		ifstream File(filename + type); 

Put braces around every conditional block, even if it contains only 1 line, otherwise you'll quickly start making errors when changing the code afterwards, it's also confusing to read without the braces/proper indentation. 


		while (getline(File, img))
		{
			cout << img << '\n';
		}

Also note the endl was changed to '\n'. std::endl is more then simply a newline, it also flushes the output buffer, which can be expensive when done in a loop like it is here, it kinda defeats the purpose of having a buffer in the first place.

Thanks on those pointers. 

 

24 minutes ago, stmfd sp!, {lr} said:

As a small side note, don't ever use system("pause"); as it's not platform independent. Use std::cin's ignore facility to block the program and then discard whatever is entered.

So what would I add to the end to make it not exit automatically? 

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9593207
Share on other sites

Link to post
Share on other sites

2 minutes ago, CookieMaster said:

So what would I add to the end to make it not exit automatically? 

Something along the lines of like:

std::cin.ignore(); does the trick.

And, if my thought-dreams could be seen,
they'd probably put my head in a guillotine.
But, it's alright, ma, it's life, and life only.

Link to comment
https://linustechtips.com/topic/758688-read-all-the-lines-c/#findComment-9593216
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×