Hello!
Yet another question of these, and trust me, i've been trying all solutions on the page regarding this topic, but nothing works.
MY ASSIGNMENT
I have a text file (.TXT) which i have already put into the Xcode "Derived Data" folder (yup using Xcode for mac). The txt file consists of 1 column, with 8 lines.
Ie:
Headline:
word 1
word 2
.
.
.
word 8
i want to ifstream the file above, read how many lines, and PRINT all the lines. Bare in mind, the words are not on 1 line, they have each of their own line. They are of different lengths, and im not supposed to know that there are 8 lines.
// print the elements in the file
cout << "Following items has to be bought:" << endl;
cout << "=========================" << endl;
while (! indFil.eof()) {
indFil.getline(&linjer, sizeof(linjer));
cout << linjer << endl;
}
return 0;
my output still only reads the numbers of lines, that the working "while" loop in "op" shows. non of the items thats in the txt. file
The reason your code from line 33 dint work is because your stream was already at EOF , you could also solve your problem by closing your stream, clear the error state then reopen your file again.
Well just a simple error , in your loop on line 26:-
1. You'll need to add elementer into the vector "linjer" linjer.push_back (elementer); /// add these on line 27
2. The reason I decided to use getline is because what if one line could have a string like
"My name" this will be a logical bug , your count would register these words as different
lines
3.
print elements in file preceded by headline and ==== line
If your file contains a headline you'll need to ignore the first reading inside
The loop.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
usingnamespace std;
int main ()
{
ifstream indFil("dosmer.txt"); // Note the open() function not needed.
string elementer, headline;
vector<string> linjer;
//kontrollerer fejl
if (indFil.fail())
{
cerr << "Åbning af fil mislykkedes!" << endl;
return(1); // Note this is main() a return is preferred over exit().
}
//Læs hvor mange linjer der er i filen
//indFil >> headline; /// ignore your headline
getline(indFil, headline); // Note I recommend getline(), no worry about spaces.
while(indFil >> elementer) // Note using the actual read to control the loop.
{
linjer.push_back(elementer);
}
// Note count no longer needed, a vector knows it's size.
cout << linjer.size() << " linjer fundet! " << endl;
cout << "\n";
// indFil.close(); // Note this close is not needed, just let the destructor do it's job.
// print elementer i filen
cout << "Følgende ting skal købes:" << endl;
cout << "=========================" << endl;
for (constauto& str : linjer)
{
cout << str << endl;
}
/* the above loop is equivalent to
* for (unsigned int j=0; j <linjer.size (); j++)
* {
* cout <<linjer [j]<<endl;
* }
*/
return 0;
}
You should strive to avoid the exit() function whenever possible in a C++ program. This C function doesn't understand C++ classes and it's use can result in data corruption.