spaces in file text

Pages: 12
I want to get an entire line of text from a file with the white spaces, but is stops at the first space. Is there any way to get the first line of text from a file with spaces?

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

using namespace std;

int main(){
   
   string fileName;
   ifstream inputFile;
   string stockName;
   
   
   cout << "Enter file name: ";
   getline(cin,fileName);
   
   inputFile.open(fileName.c_str());
   
   inputFile >> stockName;
   cout << stockName << endl;
   
   
   inputFile.close();
   
   return 0;
}
You just did it on line 15, why not try the same thing on line 19?
I tried that but it didn't like it and said there was an error. What is the proper way to write it? I was writing:

inputFile >> getline(cin,stockName);

and

inputFile >> (getline(cin,stockName));

and under line 19, but it got all the text, I just wanted the first line

getline(inputFile,stockName);
The third version in your post is correct.
how do I make it so not the entire text is read, but instead just the first line?
That is the default behavior - are you sure it's reading all lines?
yup and it doesn't read the first word. here it is:

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

using namespace std;

int main(){
   
   string fileName;
   ifstream inputFile;
   string stockName;
   
   
   cout << "Enter file name: ";
   getline(cin,fileName);
   
   inputFile.open(fileName.c_str());
   
   inputFile >> stockName;
   getline(inputFile,stockName);
   cout << stockName << endl;
   
   inputFile.close();
   
   return 0;
}
Last edited on
You don't need line 20.
but you said that was the way to do it...
I took line 19 out and now it has the first word :)
Last edited on
Oh, you forgot to remove line 19.
Sorry, my mistake.
ok so my text looks like this:

FedEx Corp (FDX)
112.57 .53 100
113.77 1.5 75

Gamestop Corp (GME)
49.21 2.2 100
49.65 1.45 75

Time Warner Inc (TWX)
65.78 -.4 100
66.2 -.6 75

I just want the first line (FedEx Corp (FDX)) any way to do that instead of the whole thing? or is there a way to add the white spaces in or maybe a way to string the next word with the first?
Last edited on
I tested your code on my machine and it worked correctly - it only grabs the first line. Are you sure nothing's wrong with your input file?
well it's just a .txt file as simple as it gets. It looks exactly like how I posted above, so i'm not sure whats wrong :(
Maybe your system is doing something weird with the line endings? Try opening it in Notepad++ and changing the line ending normalization:
http://i.imgur.com/uX9Xf0S.png
I don't have notepad, I have textedit. I tried to find how to do what you posted in textedit but couldn't.
You could try getline using a different delimiter:
 
  getline(inputFile,stockName, '\n'); // this is the default 

 
  getline(inputFile,stockName, '\r'); // try carriage return instead 
wow that worked... what is \r anyway?
Carriage Return. You probably have a really ancient mac if it uses \r but not \n.
it's only 3 years old
and I do use \n
The more I learn the more I don't like macs...
Last edited on
Pages: 12