Link to home
Start Free TrialLog in
Avatar of danieldaniel_2000
danieldaniel_2000

asked on

Infile a text line by line

How can I infile a text document to my program, without changing the format of the text?
I was trying to use string but it doesn't work, it only can infile the first line of the text. If I put the second line into the first line. It works, but I don't want to change the format of the line.
Can I do that using a for loop or any other functions.
Avatar of danieldaniel_2000
danieldaniel_2000

ASKER

I just need a simple code that can do that easily. I did try to do it, but was't able to finish it.
By "infile" do you mean "read?"

If you're using MFC, one simple solution would be the following:

CStdioFile MyFile (_T("myfile.txt"), CStdioFile::modeRead | CStdioFile::shareDenyNone, NULL);

CStringArray csMyText;
CString csOneLine;

while (MyFile.ReadString(csOneLine))
{
     csMyText.Add(csOneLine);
}

MyFile.Close();

-----

When you're finished with the loop, the CStringArray (csMyText) will contain all of the contents of the file, one line per array element.

 It's possible to do this without MFC, but it is more complicated.  Let me know if you need that solution.
Do you want to input the file to another file or do you want to put parts of the infile into variables in your program.  Please specify.



homer99 changed the proposed answer to a comment
If you mean you are #including the text file to be part of a string.  Then you will have problems when the compiler finds a CR.  The only way you can fix this is to put the whole string on one line or by putting a \ at the end of each line.  I cannot think of any other way......
#include <string>
#include <fstream>

using namespace std;

int main()
{
   fstream Fil("filename.txt",ios_base::in);
   string Txt;

   while (!Fil.eof())
   {
      string Lin; // Current line read.

      getline(Fil,Lin); // read the next line
      if (Txt.length()) // If there was a previous line,
         Txt += '\n'; // Add a newline to the previous text.
      Txt += Lin; // Add the current line to the text.
   }
   return 0;
}
That reads the entire text into a single strign an seperates each line with a newline character.  Another options would be to read each line in as store each line in a seperate string  that is in a vector<> of strings.   Personally would prefer that approach.
The comment from nietod is quiet satifying but I am not quiet sure about the namespace std, Can you explain it?
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Adjusted points to 101
To nietod:
Sorry to disturb. What do you mean by using "string text". Also I want to ask that how I can use vector of string.
Like this:
vector<char>My vector(number of lines);

Can you give me an example?
For nietod:
other than fstream and string, what do I need to include, and what do you mean by include the std namespace.
>> What do you mean by using "string text"
"string" is the name of a class defined in the standard template library.  the class is used for storing character strings.  i.e

string MyName = "Daniel";

stores the character string "Daniel" in the object called MyName.

>>  how I can use vector
>> of string.

#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
   fstream Fil("filename.txt",ios_base::in);
   vector<string> Txt;

   while (!Fil.eof())
   {
      string Lin; // Current line read.

      getline(Fil,Lin); // read the next line
      Txt.push_back(Lin); // Append the line to the array.
   }
   return 0;
}

This fills vector with lines of text.  Operator [] can be used to access each line, like "Txt[0]" returns the first line, "Txt[1]" the second line, etc.

>> nclude the std namespace
that means all the names in the "std" namespace will be be "included" in your namespace, so you don't have to prefix the names with "std::".  This it saves you from having to type all those "std::"s.   However, it won't work if you have used some of these names in your progam for other purposes.  for example if you did

char string[10];

then the name "string" in the std namespace, whichis te name of a class  would conflict with the name "string" in your program which is the name of a variable. and the compiler couldn't compile the code.  It would be the same as doing

int age;
int age;
double age;

You have 3 varaibles all declared with the same name.  That isn't allowed.  The compiler can't tell the appart.  but if you did

namespace one
{
  int age;
}

namespace two
{
   int age;
}

namepace three
{
   int age;
}

the compiler could tell them appart, because the names have to be prefixed by "one::", "two::"or "three::".  But if you included two of those namespaces into the current namespace, you'd have a problem again.
To nietod:
For "include string", is that mean #include <string.h>
I try to do this, and the compiler give me error.
Do you mean string.h or other string class?
To nietod:
For "include string", is that mean #include <string.h>
I try to do this, and the compiler give me error.
Do you mean string.h or other string class?
Do I need to write the whole class out?
>> For "include string", is that mean
>> #include <string.h>
It should be

#include <string>

with no .h.  This will not work if your compiler is out-of-date, (say more than 5 years out-of-date).  If it is that old, you really need to upgrade!
What compiler are you using?   what OS?
My compiler is microsoft visual C++ 6.0
In my compiler, it has string.h, not string.

You should definatly have a <string> header file aswell, unless you have deleted it....  Do a file find on your installation directory for Visual Studio it should find it in vc98\include


   
>> My compiler is microsoft visual C++ 6.0
>> In my compiler, it has string.h, not string.
as cwques said, you should have both.  The <string.h> include is an old C include.  <string> is the new C++ include and you really should have it.  

What error message do you get when you try to use it.
I get like undeclared idenfier string
I check it i don't have the <string>
I check it carefully and i found out that there is a file calls string but it is like a blanksheet, i check the properties of the file, it says string.lnk. Can I still include <string>, because I think it is not going to work out.
>> I get like undeclared idenfier string
That is not sayign it can't find the include file <string>.  It is finding it.  Otherwise you would get an error message that says that it can't find the file.

Do you have a "using namespace std;" in there?