-
March 2nd, 2011, 03:24 AM
#1
Reading from specific row on notepad
Hi! Is it possible to read from specific rows from a notepad? I'm using Borland C++
For Example:
Jay
Mark
Christian
I only want to display the word Christian which is on the third row, what will I do? can you please show me? Thanks! Here's my code:
#include <iostream.h>
#include <iomanip.h>
#include <fstream.h>
#include <cstring.h>
#include <string.h>
#include <conio.h>
//using namespace std;
int main()
{
char account[80];
char string1[] = ".txt";
char string2[17];
cout<<endl;
cout<<"Enter Account Number: ";
cin>>account;
strcpy(string2, account);
strcat(string2, string1);
int bal = 0;
int x;
ifstream inFile;
inFile.open(string2);
if (!inFile) {
cout << "Invalid Account";
exit(1); // terminate with error
}
while (inFile >> x)
{
bal = bal + x;
}
inFile.close();
clrscr();
cout<<endl;
cout<< bal << endl;
getch ();
return 0;
}
-
March 2nd, 2011, 04:14 AM
#2
Re: Reading from specific row on notepad
A file is really nothing more than an array of characters to a computer. It is not possible to "skip" to a given line, unless you know its start index. What you can do though is read the file and count the lines until you reach the line you want. In particular, you can use getline n times.
That said, you need to use a newer compiler. iostream.h ain't no header I've ever heard of (amongst other problems).
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
March 2nd, 2011, 04:21 AM
#3
Re: Reading from specific row on notepad
I don't understand what the code you posted has to do with your question. They seem unrelated. One thing I'll point out is that your Borland compiler must be over 10 years old. Library headers that end with ".h" are not a part of the C++ standard. Conio.h is very much depreciated as it comes from the days of DOS, and was never a part of any standard. I suggest you get a modern compiler, they aren't hard to find and generally cost nothing.
Code:
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main()
{
ifstream myFile("myfile.txt");
vector<string> lines;
string line;
while(getline(myFile, line))
{
lines.push_back(line);
}
cout << lines[2] << endl;
return 0;
}
-
March 2nd, 2011, 09:47 PM
#4
Re: Reading from specific row on notepad
I know Borland C++ is an old compiler, but as much as i want to use another program, i cannot, because this what my professor is using, we can only choose from Borland and Turbo c++
-
March 2nd, 2011, 10:42 PM
#5
Re: Reading from specific row on notepad
Looks like your professor is going to teach you how he did tricks in the stone age. Using the same tools.
Nobody is actually forcing you to use it at your own time. You can always compile your code with both the old and current compiler and see what fails where. At least you'll learn what changed.
-
March 2nd, 2011, 11:07 PM
#6
Re: Reading from specific row on notepad
Originally Posted by jaydee_ado
I know Borland C++ is an old compiler, but as much as i want to use another program, i cannot, because this what my professor is using, we can only choose from Borland and Turbo c++
Well if your professor isn't going to teach using an ANSI C++ compiler, then you probably shouldn't be taking his class at all. I think you should bring up this fact with him as it's pretty serious. Would you stick around if the professor was teaching earth science out of a book that said the world was flat?
Last edited by Chris_F; March 2nd, 2011 at 11:13 PM.
-
March 3rd, 2011, 05:58 AM
#7
Re: Reading from specific row on notepad
1st of all use code tags. and for reading from particular line you need to set file pointer to that particular point and then start reading.
read char by char and if '\n' found ie: the line break increment an int counter by 1. now set the file pointer on the desired count ie: line no.
and really there is no link between your qus and posted code.
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
|