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.
danieldaniel_2000Asked:
Who is Participating?
 
nietodConnect With a Mentor Commented:
It is optional--in a sense.  The stl classes (and other "stuff") are placed in a namespace called "std".  namespaces are a "scope" used to prevent names  declared within the namespace from conflicting with the same names declared elsewhere (for other purposes).  i.e. it allows a single name to be used to name two different things, as long as they are declared in different namespaces.  The STL designers placed all the names used in the STL in a namespace called "std" so that the names they use will not conflict with the names you use.  This is very important because they used a lot of popular names--like "string", so if they didn't use namespaces programs would not be able to use these names for their purposes.

When a name is declared in a namespase, you must prefix its name with the namespace's and and the scope resolution symbol ("::")  so to define an STL string class object you would do

std::string Line;

But all those "std::"'s running around can get annoying.  So if you don't use any names that are defined in the stl, you can just include the std namespace into yours using a "using declaration", like

using namespace std;

This allows you to access all the names in the specified namespace, without prefixing their names with the namespace names.
0
 
danieldaniel_2000Author Commented:
I just need a simple code that can do that easily. I did try to do it, but was't able to finish it.
0
 
brandtsCommented:
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.
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.

 
homer99Commented:
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.



0
 
homer99Commented:
homer99 changed the proposed answer to a comment
0
 
cwguesCommented:
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......
0
 
nietodCommented:
#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;
}
0
 
nietodCommented:
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.
0
 
danieldaniel_2000Author Commented:
The comment from nietod is quiet satifying but I am not quiet sure about the namespace std, Can you explain it?
0
 
danieldaniel_2000Author Commented:
Adjusted points to 101
0
 
danieldaniel_2000Author Commented:
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?
0
 
danieldaniel_2000Author Commented:
For nietod:
other than fstream and string, what do I need to include, and what do you mean by include the std namespace.
0
 
nietodCommented:
>> 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.
0
 
danieldaniel_2000Author Commented:
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?
0
 
danieldaniel_2000Author Commented:
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?
0
 
nietodCommented:
>> 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!
0
 
nietodCommented:
What compiler are you using?   what OS?
0
 
danieldaniel_2000Author Commented:
My compiler is microsoft visual C++ 6.0
In my compiler, it has string.h, not string.

0
 
cwguesCommented:
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


   
0
 
nietodCommented:
>> 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.
0
 
danieldaniel_2000Author Commented:
I get like undeclared idenfier string
I check it i don't have the <string>
0
 
danieldaniel_2000Author Commented:
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.
0
 
nietodCommented:
>> 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?
0
All Courses

From novice to tech pro — start learning today.