0

This is the code I am using to print output from my textfile

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

int main()
{
string line[30];
ifstream myfile("input1.txt");
int a = 0;

if(!myfile)
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}  
while(!myfile.eof())
{
getline(myfile,line[a],' ');
cout<<line1[a]<<"\n";

}
}

The text output is supposed to be:(Also the output is exactly the same as the input)

ABCDEFGHIJKLMNOPQRSTUVWXYZ

9876543210

MFCJABDEHGIKLTPNORSQWUVYXZ

SPHINCTERSAYSWHAT

524137968

MATLSO

FTERFO

EYBLEIF

LYBWIOL

SYGTHO

FPNOEDESO

LLTDREOI

But instead I get this output:

ABCDEFGHIJKLMNOPQRSTUVWXYZ

9876543210

MFCJABDEHGIKLTPNORSQWUVYXZ

SPHINCTERSAYSWHAT

524137968

MATLSO

(double space between these two)

FTERFO

(double space after this one)

EYBLEIF LYBWIOL(EYBLEIF on top and LYBWIOL directly under it with no new line)

SYGTHO FPNOEDESO LLTDREOI(all of these from top to bottom directly underneath each other)

user3358064
  • 7
  • 1
  • 6
  • I'd post pictures of the output instead but i need 10 reputation – user3358064 Feb 27 '14 at 10:20
  • Firstly, copy your input file here too; secondly, have a look at http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – stefaanv Feb 27 '14 at 10:24
  • @stefaanv sorry i forgot to mention the output is meant to be exactly the same as the input – user3358064 Feb 27 '14 at 10:29
  • 1
    Rule of thumb: if you call `eof`, you've got a bug. – molbdnilo Feb 27 '14 at 10:30
  • problem is input file itself mostly. your code is fine. since you used space as delimiter make sure that no extra space is in input file. and that cout< – LearningC Feb 27 '14 at 10:31
  • If the output must be the same as the file, then the input and output separater should be the same. Now input seaprator is ' ' and output separater is "\n". – stefaanv Feb 27 '14 at 10:34

2 Answers2

1

From docs

istream& getline (istream&  is, string& str, char delim);
Get line from stream into string
Extracts characters from is and stores them into str until the delimitation character delim is found.

It seems that you have two extra spaces after "MATLSO" and "FTERFO". Remove them.

UPDATE: "delimitation character" in your code is ' ' so "MATLSO " will be treated as several lines, not one.

Community
  • 1
  • 1
Avt
  • 16,037
  • 4
  • 48
  • 67
1

The space is the delimiter for getting another line of text. So if you have trailing spaces at the end of lines in the input file you will get some additional blank lines in the output.

Sometimes it's useful to view your input file in a text editor and turn on control characters so that you can see any additional spaces at the end of lines. For example in VI editor I use "set list" to see control characters (line endings, tabs etc).

To help process your input file you might want to cleanse it (remove trailing spaces) before parsing it.

Simon Bosley
  • 1,016
  • 2
  • 17
  • 38
  • Wow it was like invisible spaces in the input file, thank you lol – user3358064 Feb 27 '14 at 10:35
  • No worries @user3358064 glad you found it! – Simon Bosley Feb 27 '14 at 10:48
  • @stefaanv Agreed, it would be more stable if it could handle this situation. Some indentation in code would be nice to, I assume that was a copy+paste problem :-) – Simon Bosley Feb 27 '14 at 10:54
  • Could i ask one more question lol, if i wanted to reverse the output couldn't i just use a loop like `for (a = 30-1; a>=0; a--) cout<< line1[a]< – user3358064 Feb 27 '14 at 11:03
  • Yes you would read in the file in one for loop as you have, then you could print out the contents in reverse from your array in a second for loop. If the contents of your file is less than 30 lines you will end up with blanks at the start. You might want to do a line count as you're reading from the file so that you know how many lines you've read. Then you can do: for (a = linecount-1; a>=0; a--). It would probably make more sense to store your lines of text in an std::vector so that you're not limited to a max size of 30. – Simon Bosley Feb 27 '14 at 12:10
  • It would make more sense to push_back() the strings in a std::vector and then do a reverse iteration. Then you are not limited to 30 lines and you don't have to keep track of how many lines were read. – stefaanv Feb 27 '14 at 12:38