reading lines from a text file and storing it in array

Below is a simple code which is reading characers from a file and storing each character in an array.

I want to convert this code to read line instead of a character and store line into the array

thanks


#include <fstream.h>
int main()
{
      char filename[80] = "tester1.txt";
      ifstream fin(filename);
      char ch;
      char array[100000];
      int i=0;
      while(fin.get(ch))
      {
      array[i++]=ch;
      cout<<ch;
      }
      fin.close();
      for (int k=0;k<i;k++)
      cout<<array[k];
      return 0;
}
tparvaizAsked:
Who is Participating?
 
cdesignerConnect With a Mentor Commented:
fgets, fgetws
Get a string from a stream.

char *fgets( char *string, int n, FILE *stream );

wchar_t *fgetws( wchar_t *string, int n, FILE *stream );

Function Required Header Compatibility
fgets <stdio.h> ANSI, Win 95, Win NT
fgetws <stdio.h> or <wchar.h> ANSI, Win 95, Win NT
0
 
nietodCommented:
That is the C way of doing things, I would strongly (extremely strongly) recommend you switch to the C++ way, which is to use file I/O stream objects.  There are many advantages to these.  In your case, for example, they can be used to read a line of any length (storage space expands to hold the length of the line, that is not possible with the C stream procedures, like fgets().
0
 
tparvaizAuthor Commented:
nietod, could you please post your suggestion as an edited code (edit my code please)

cdesigner: my requirement should be related to my code, please let me know if you have any other suggestion regarding my question.

Thanks for your time friends
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
nietodCommented:
I didn't read carefully enough!  tparvaiz, you are already using C++ file stream objects.  You definitley don't want to switch back to C File streams as suggested, that is a step in the wrong direction.  Keep using file stream objects.

#include <fstream>
#include <vector>
#include <string>
using namespace std; // May or may not want this.

int main()
{
   char filename[80] = "tester1.txt";
   ifstream fin(filename);

   vector<string> array;  // you could use "string array[100]"
                    // but a vector is best if you don't know the size.

   while(!fin.eof())
   {
       string Line;
       getline(fin,Line); // read a whole line.
       array.push_back(Line); // Append line to array of lines.
   }
   fin.close();

   const int LineCount = array.size();
   
   for (int k=0;k< LineCount; k++)
        cout<<array[k] << endl;

    return 0;
}
0
 
tparvaizAuthor Commented:
after deleting this line
using namespace std; // May or may not want this

I am having these error messages:


readingfile.cpp(9) : error C2065: 'ifstream' : undeclared identifier
C:\readingfile.cpp(9) : error C2146: syntax error : missing ';' before identifier 'fin'
C:\readingfile.cpp(9) : error C2065: 'fin' : undeclared identifier
C:\readingfile.cpp(11) : error C2065: 'vector' : undeclared identifier
C:\readingfile.cpp(11) : error C2065: 'string' : undeclared identifier
C:\readingfile.cpp(11) : error C2065: 'array' : undeclared identifier
C:\readingfile.cpp(11) : warning C4804: '>' : unsafe use of type 'bool' in operation
C:\readingfile.cpp(11) : warning C4552: '>' : operator has no effect; expected operator with side-effect
C:\readingfile.cpp(14) : error C2228: left of '.eof' must have class/struct/union type
C:\readingfile.cpp(14) : fatal error C1903: unable to recover from previous error(s); stopping compilation
Error executing cl.exe.
0
 
nietodCommented:
vectors, streams, strings and many other things are defined in the STL namespace.  You must either specify that namespace when you usie these things or you must us a using declaration (like you removed).  So leave that using namespace std line or specify "std::" before these names, like

  std::ifstream fin(filename);

 std::vector<std::string> array;  // you could use "string array[100]"
0
 
nietodCommented:
What is happening with this question?
0
All Courses

From novice to tech pro — start learning today.