file lines?

This is a discussion on file lines? within the C++ Programming forums, part of the General Programming Boards category; again, im working on a parser, is there way to determine what line of code your on. I was thinking ...

  1. #1
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    file lines?

    again, im working on a parser, is there way to determine what line of code your on.
    I was thinking of writing something that goes through the whole file, and checks for '\n's that would work right?
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is there way to determine what line of code your on
    std::cout<< __LINE__;

    -Prelude
    My best code is written with the delete key.

  3. #3
    Comment your source code! Lynux-Penguin's Avatar
    Join Date
    Apr 2002
    Posts
    533

    new question

    ok I solved that problem now, in a file, how can I go directly to a line?

    Here is what I came up with for the number of lines
    Code:
    #include <iostream.h>
    #include <fstream.h>
    #include <stdlib.h>
    
    int main(int argc, char* argv[])
    {
    	if(argc>1)
    		abort();
    	char a;
    	int count=0,lines=0;
    	ifstream in;
    	ofstream out;
    	in.open("c:\\luke\\new.txt");
    	out.open("c:\\luke\\new2.txt");
    	while(in.get(a))
    	{	if(a=='\n')
    			lines++;
    	}
    	cout<<lines<<endl;
    	in.close();
    	out.close();
    	return 0;
    }
    the out file is used later
    Asking the right question is sometimes more important than knowing the answer.
    Please read the FAQ
    C Reference Card (A MUST!)
    Pointers and Memory
    The Essentials
    CString lib

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >how can I go directly to a line?
    If you want to be gutsy you can format the file so that the offsets are properly sequenced and then you can seek to the line you want, something like this:

    file.seekg ( line_num * LINE_BYTES );

    Or you could keep it simple and just read your way to the line.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >You can only use seek() to get to a line if you've previously produced an index.
    This is true for text files, but binary files are not restricted in this manner (I'm sorry for not being more clear on what I meant). According to the C++ standard, fstream::seekg calls filebuf::seekoff which in turn calls the equivalent to fseek. fseek is defined as follows for binary and text streams:
    Code:
    For a binary stream, the new position, measured in characters from the beginning of the
    file, is obtained by adding offset to the position specified by whence. The specified
    position is the beginning of the file if whence is SEEK_SET, the current value of the file
    position indicator if SEEK_CUR, or end-of-file if SEEK_END. A binary stream need not
    meaningfully support fseek calls with a whence value of SEEK_END.
    
    For a text stream, either offset shall be zero, or offset shall be a value returned by
    an earlier successful call to the ftell function on a stream associated with the same file
    and whence shall be SEEK_SET.
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-22-2008, 12:19 AM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21