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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

cdesignerCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.