LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-10-2009, 08:02 PM   #1
Mork2k4
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Rep: Reputation: 0
Reading specific lines from a file


Being all the threads I saw were old I didn't want to raise the dead. I have one more question.

To save on the writing of WAY to many files with very little in them, I want to put it all in one file and read a specific few lines. There will be six variables to be read at a time. Format is as such:

//Set 1
string name
5
12
8
15
9

//Set2
string name
12
7
9
23
19

From name to 5th number is a set. The name will be of different lengths for each set. This will be a big file of probably 40+ sets. My problem lies in reading one and only one set be it set 5 or set 34. It needs to be done in C++.
 
Old 09-10-2009, 10:31 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
you cant read certain lines of a text file, without reading the contents up to (if reading from the start) or the contents after (if reading from the end). similarly, on a set of 10 stairs, you cant walk (i.e. not jump) up to stair 5 without going to stairs 4, 3, 2, 1.

since you have some structure to your data, why not use a defined data structure? since it is just that--data--and it doesnt have logic, you could define a struct (opposed to a class, which has logic). if you use a data structure, such as a struct, you can directly access read specific structs (or "sets" as your calling your data structure). the only thing is, you must define a maximum value for the "string name", which you didnt say whether this is a problem or not (besides the fact that the names are variable length). you might define your structure this way:
Code:
struct Set
{
  char name[30];
  int values[5];
};
you can then use c++ file IO classes (input: ifstream; output: ofstream) to do binary input and output, respectively. it is binary IO because you are dealing with the atomic (i.e. cannot be broken down) "Set" type you create, rather than the atomic type of "character", as your file is now. also, when viewing the file in some plain-text editor, it is inherently unreadable because it is binary information, not a string of characters as your file is now.

using a struct and the fstream classes, if you wanted to read only the 34th "Set", you could do:
Code:
Set myEntry;

// here declare an 'ifstream' object, 'myFile', for binary input, see the documentation for it

myFile.seekg( 34 * sizeof(Set), ios::beg); // from the beginning of the file, go to the 34th "Set" entry (determined by the number of bytes, i.e. 34 * num bytes of "Set")

myFile.read( &myEntry, sizeof(Set) );  // EDIT: i forgot to mention, you will have to use 'reinterpret_cast' for this, search around for example
if you do it this way, the input of course must be the same format (binary struct). therefore you would have to, once, read the file line by line (linearly, as you probably are now), create a "Set" from each set of 6 lines, or whatever it is, and write all of these "Set"s out to a binary file. after you have the file in the correct format, you can read/write in constant time (i.e. to read the first set takes the same amount of time to read the 1000th set, or Nth set).


however, you could do a similar thing and use plain text files. you would still need to define a data structure (it could just be logical, and not actually any "struct" or "class"). because of this, as with structs, your strings must be a constant size and padded with '\0' null characters or whatever you want, in order to make every "string name" line the same size, say 10 (or whatever) characters
Code:
m y N a m e \0 \0 \0 \n
. doing it like this, keeping it plain text with no binary information or data structures, probably isnt the best approach ('structs' may be). with plain-text, it boils down that every Nth row must be the same length (or padded with information to make it that length). every 6th line (from line 1) must be the same number of characters long. every 6th line (from line 2, i.e. the first number in each set) must be the same number of characters, if your number is 5, you must pad it to make it constant length, such as 005 (if you had numbers such as 123, etc).

i hope this makes sense to you, but, again, the short answer is that you cant go to line N directly. and again, i would recommend structs or some binary data structure.

Last edited by nadroj; 09-11-2009 at 12:01 AM.
 
Old 09-10-2009, 11:51 PM   #3
Mork2k4
LQ Newbie
 
Registered: Sep 2009
Posts: 8

Original Poster
Rep: Reputation: 0
Yes it makes sense. Thank you very much. Being that its so late here I will try it tomorrow when I wake up and see how things go. Thanks again for the help
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing specific lines of a file using script. barunparichha Linux - Software 6 05-20-2009 12:31 AM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM
Read specific lines from a text file chobin Programming 8 06-14-2006 11:14 AM
PHP - Help With Reading Lines In A File windisch Programming 6 05-03-2006 12:48 PM
Reading the number of Lines in a File Mistro116@yahoo.com Programming 31 11-24-2005 12:36 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:02 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration