codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  read lines from a file  rastaberry at 12:38 on Monday, February 23, 2009
 

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;}


  Re: read lines from a file  jinhao at 20:21 on Tuesday, April 28, 2009
 

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;
}








CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  Multiple onload not working?
•  Hi,i m a new guy learing c++, here is the thing...please help me out...
•  Re: read lines from a file
•  Re: C++ Inheritance Polymorphism
•  Re: getting an object`s property which is decided by content of variable.
•  Re:
•  Re: The date
•  Re: tables
•  Re: java who needs it


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2009