|
-
March 8th, 2011, 02:46 AM
#1
reading a line from text file
// text_read.cpp
// compile with: /clr
#using<system.dll>
using namespace System;
using namespace System::IO;
#include <iostream>
using namespace std;
int main()
{
String^ fileName = "e:\\test.txt";
try
{
//Console::WriteLine("trying to open file {0}...", fileName);
StreamReader^ din = File::OpenText(fileName);
String^ str;
int count = 0;
while ((str = din->ReadLine()) != nullptr)
{
count++;
Console::WriteLine("line {0}: {1}", count, str );
}
}
catch (Exception^ e)
{
if (dynamic_cast<FileNotFoundException^>(e))
Console::WriteLine("file '{0}' not found", fileName);
else
Console::WriteLine("problem reading file '{0}'", fileName);
}
system("pause");
//std::cout << "Paused, press ENTER to continue." << std::endl;
//cin.ignore(1000000);
return 0;
}
with that above code i am able to read from a text file and display the content. But it displays the whole content of the file. Please help me modify the codes so that I can only display one particular line.
Help will really be appreciated
-
March 8th, 2011, 02:52 AM
#2
Re: reading a line from text file
I didn't check you code, for reading line wise use this code..
Code:
char line[100];
ifstream in("file.txt");
while (!in.eof())
{
in.getline(line, 200, '\n');
}
in.close();
And from next time post simple C/C++ query in C/C++ forum not in VC++ forum..
-
March 8th, 2011, 02:57 AM
#3
Re: reading a line from text file
put a break statement within the while loop after reading one single line.. it will come out after displaying one line..
-
March 8th, 2011, 03:00 AM
#4
Re: reading a line from text file
 Originally Posted by vcdebugger
put a break statement within the while loop after reading one single line.. it will come out after displaying one line..
could you please show me how it is done..
thank u
-
March 8th, 2011, 03:06 AM
#5
Re: reading a line from text file
You dont know how to use 'break' and whats the use of break.
If not, then go and read some C books before posting further.
-
March 8th, 2011, 03:53 AM
#6
Re: reading a line from text file
Your code is managed C++, not native C++. Try asking your question here.
-
March 8th, 2011, 03:55 AM
#7
Re: reading a line from text file
Is this to do with your previous post regarding the setting of controls using a text file if it is the code I attached to my last response reads the file and retreives the required data just fine.
Please advise of solution it makes it easier to find an answer.
-
March 8th, 2011, 04:18 AM
#8
Re: reading a line from text file
 Originally Posted by Skizmo
Your code is managed C++, not native C++. Try asking your question here.
Oh yes. You are right. I didn't even check those namespace before posting an ans.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|