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, does anybody 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[50];
    FILE*pFile;
    int line=0;
    pFile = fopen ("text.txt" , "r");
    
    if (pFile == NULL) perror ("Error opening file");
    
    else {
    for (int i=0; i<50; i++) {
    while(fgets(retazce[i], 100, pFile)!=NULL) { 
    fgets(retazce[i], 100, pFile);
    line++;}}
}
for (int i=0; i<50;i++)
    cout<< retazce[i] <<"toto bol retazec" << endl;

cout<<"pocet riadkov "<< line<<endl;

cin.ignore();
cin.ignore();
return 0;}

Recommended Answers

All 12 Replies

Why not use a vector<string>? Then you can use getline() :)

lines 16, 17 and 18: too many loops and too many fgets() function calls. The while statement will read the entire file.

Also you can use retazce like that because its just an array of pointers that point to nowhere. You have to first read the line into a valid buffer, allocate space in retazce, then copy to retazce, like below

char oneline[255];
while(fgets(oneline, sizeof(oneline), pFile)!=NULL) { 
   retazce[line] = new char[strlen(oneline)+1];
   strcpy(retazce[line], oneline);
   line++;
}

Ok, I see, however this makes the same wrong output

#include <stdio.h>
#include <iostream>

using namespace std;

int main()
{   
    char*retazce[70];
    FILE*pFile;
    int line=0;
    pFile = fopen ("pokus.txt" , "r");
    
    if (pFile == NULL) perror ("Error opening file");
    
    else {
    for (int i=0; i<70; i++) {
    while(fgets(retazce[i], 100, pFile)!=NULL) 
    line++
}
for (int i=0; i<5;i++)
    cout<< retazce[i] <<"this was a line" << endl;

cout<<"number of lines "<< line<<endl;

cin.ignore();
cin.ignore();
return 0;}

you failed to implement everything I said before. Re-read my previous post and compare with what you posted. You are trashing your program's memory with that fgets() line

I am very sorry somehow I have notice only a half of your reply. I will try your suggestion.

Ok, thank you very much.
No the output is almost correct, however, there is always a seperate empty row beteen to lines in the output. I guess this has to do something with the fact that the getline reads the '\n' sign.

Is there please anyway I could get rid of that?

fgets() function appends '\n' to the end of the string, so you either need to strip it from the input string or not print '\n' (endl) in the output string.

But look how easy this is!

ifstream fin(Filename.c_str());

	if(fin == NULL)
		cout << "Cannot open file." << endl;

	vector<string> Lines;
	string line;
	
	while(getline(fin, line))
	{
		Lines.push_back(line);
	}
	
cout << "NumLines: " << Lines.size() << endl;

	for(unsigned int i = 0; i < Lines.size(); i++)
		cout << Lines[i] << endl;

Thanks a lot for this I will experiment with that!

I would need the lines read in the form of
char*pointer[10] ///in this form I have written the rest of the program,which works, so I try to avoid changing it
this.

I tried this, but it doesn't do the right job-> in the pointer[0] is nothing at the end, can anybody please explain why?

#include <fstream>
 #include <iostream>
 #include <stdlib.h>

using namespace std;
int main() 
{   char*pointer[10];
ifstream fin("pokus.txt");
 
    int MAX_LENGTH = 100;
    char line[MAX_LENGTH];
    
 
   
      for (int i=0; i<10; i++) {
         fin.getline(line, MAX_LENGTH);
         cout << "read line: " << line << endl; 
          pointer[i]= line;
          }
      
cout<< "pointer[0] " << pointer[0]<< endl;
cout<< "*pointer[0]" << *pointer[0];
cin.ignore();
cin.ignore();
}

You are not listening -- I already explained that 6 hous ago in my post #3 to this thread.

As I alredy mentioned and posted you have to allocate memory for those pointers in that char*pointer[10] array. The way you have written it all 10 pointers point to the same address. What you need is for each pointer to have different, unique addresses. The only way to accomplish that is using the new operator.

thanks, finally I solved it by this

for (int i=0; i<MAX_RADKU; i++) {
       fin.getline(line, MAX_DLZKA);
       retazce[i] = new char[MAX_DLZKA];
       strcpy(retazce[i], line);
       }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.