|
hello, I want to read lines into an array (I will later work with the array of pointer , so that is why I use it)
well this is my code that doesn't work correctly, it gives a wrong output. Does anybody please now why?
I created a simple txt document for this which has only a few lines
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
char*retazce[70];
FILE*pFile;
int line=0;
pFile = fopen ("/forum/pokus.txt" , "r");
if (pFile == NULL) perror ("Error opening file");
else {
for (int i=0; i<70; i++) {
while(fgets(retazce, 100, pFile)!=NULL)
line++
}
for (int i=0; i<5;i++)
cout<< retazce <<"this was a line" << endl;
cout<<"number of lines "<< line<<endl;
cin.ignore();
cin.ignore();
return 0;}
|
|
|
It is easy to use a file stream.
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
int main()
{
using namespace std;
vector<string> retazce;
ifstream ifs("/forum/pokus.txt");
if(ifs)
{
string lnstr;
while(ifs.good())
{
getline(ifs, lnstr);
retazce.push_back(lnstr);
cout<<lnstr<<endl;
}
cout<<"number of lines "<<retazce.size()<<endl;
}
else
cerr<<"Error opening a file"<<endl;
}
|
|
|
|
|
|
|
// |