Reading from specific row on notepad
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Reading from specific row on notepad

  1. #1
    Join Date
    Feb 2011
    Posts
    3

    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;
    }

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,276

    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 LITE 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.

  3. #3
    Join Date
    Aug 2008
    Posts
    902

    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;
    }

  4. #4
    Join Date
    Feb 2011
    Posts
    3

    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++

  5. #5
    Join Date
    Sep 2010
    Posts
    31

    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.

  6. #6
    Join Date
    Aug 2008
    Posts
    902

    Re: Reading from specific row on notepad

    Quote Originally Posted by jaydee_ado View Post
    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 10:13 PM.

  7. #7
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center