Computing.Net > Forums > Programming > Reading Specific Lines in C++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Reading Specific Lines in C++

Reply to Message Icon

Name: Larry
Date: August 10, 2002 at 20:11:56 Pacific
Comment:

I want to be able to read a specific line from a file in C++. I currently have a program that goes like this:

char name[20];
int main(int argc, char *argv[])
{
cout << "Please enter your name: ";
ofstream examplefile ("example.txt", ios::out | ios::trunc);
cin.getline (name, 20);
examplefile << name;
examplefile.close();

return 0;
}

And so far, it compiles fine but I want to be able to read and write to a specific line in the text document. Such as when the user enteres their name it would be written to line 2 instead of the top line and then I would like to be able to read from line 2 specifically later on in the program.

Please help!



Sponsored Link
Ads by Google

Response Number 1
Name: peter4u
Date: August 11, 2002 at 14:50:36 Pacific
Reply:

You are actually asking for a database solution with a table and an index. The next question is, if you are going to use a fix or variable record length and how do you map the name to get an unique index.

If that's all too complicated, you may just want to stick with with your sequential search.


0

Response Number 2
Name: Shani Khan
Date: August 17, 2002 at 03:36:56 Pacific
Reply:

someone told me a question that make a C++ program in which user can read the data from data in file,whose first line has an integer value that show that how many next line s you have to read.And every line has two integer values and display,you have to find the maximum of those,both values and display the input values and the maximum of both.


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Reading Specific Lines in C++

echo text to specific line in file www.computing.net/answers/programming/echo-text-to-specific-line-in-file/11965.html

batch edit specific line in txt www.computing.net/answers/programming/batch-edit-specific-line-in-txt/16778.html

Reading specific line by FROTRAN www.computing.net/answers/programming/reading-specific-line-by-frotran/14124.html