468,512 Members | 1,415 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Read exact line from ASCII file

Hello!
If anyone can help me... I have ASCII file with statistic data, and need to read only one line from it, how can I indicate, which line I want to read and then print in another file...???

Example:
File: "xxx.ASCII"
Lines in it:
(1,65) 22/11/2007 12:00 {29 0 0} 0 5 85 69 55
(1,64) 22/11/2007 12:00 {29 5 9} 5 6 2 7 9
(1,65) 22/11/2007 12:00 {29 10 1} 58 6 98 5 45
(1,55) 22/11/2007 12:00 {29 10 1} 55 56 59 22 21

I want to read only third line from this file and write it to file "output.txt"... How can I do that...??? I tried to find solution, but without results... Im new in C++, so be patient... Thanks...!!!
Nov 20 '07 #1
10 7252
AHMEDYO
112 100+
HI...

you can use fstream class to read the file lines,line by line, but i think there is no way to select the line and then get it, i think you must read lines and use counter in your code to take special action when it equal 3.

Best Regards
Nov 20 '07 #2
weaknessforcats
9,207 Expert Mod 8TB
I would call file.getline() three times and go with the result. The "file" is an ifstream variable that you would create.

You can't use the >> operator since that won't read an entire line.
Nov 20 '07 #3
Mmm... I think I didn't tell all information... SRY...!!! The file I need to read is VERY big... Maybe there is some possibility to find the line using the begin of line or other cross-reference from the line...???

Actually the keywords are:
(1,65) 22/11/2007 12:00 {29 10 1} 56 59 54 54

(1,65) is number of counter and {29 10 1} is one element... IN the ASCII file is more than 100 element's and for each element is more than 100 counters... And from all of them I need to find exactly this one...
Nov 20 '07 #4
AHMEDYO
112 100+
HI..

if you know what exactly the line number is , for example if the line is number 3 then call getline function 3 times, and then use strstr(str,substr) to search about substring within your line.


Best Regards
Nov 20 '07 #5
HI..

if you know what exactly the line number is , for example if the line is number 3 then call getline function 3 times, and then use strstr(str,substr) to search about substring within your line.


Best Regards
Nope... I don't know line number... I get the files each hour and each hour I need to write this line in another file... And each hour this line can be in different position in ASCII file...
Nov 20 '07 #6
AHMEDYO
112 100+
Hi..

i think your problem is the file is so big,,right??, then you mean you cant go each hour to loop through all lines within the file, if so, i think you have solution, if this file is increased by appending new lines at the end of file, i think you can search all file lines once and create counter variable that hold use fstream.gcount() function to get readed bytes count for example
Expand|Select|Wrap|Line Numbers
  1.  CurrentlinePos+=(fstream.gcount()+1) 
, and when you complete reading file save this value external from your project in registry or in external file, and then each time you will go to search within the file again you can use fstream.seekg(CurrentlinePos) to start from the last position you was stop with the last search operation and when you complete increase CurrentlinePos Again with last position, then i think we ignore BIG file problem, and as i was descript you can search about counters and item number using strstr function

Best Reagards
Nov 20 '07 #7
Nope... I don't know line number... I get the files each hour and each hour I need to write this line in another file... And each hour this line can be in different position in ASCII file...

The easy way would be using the power of scanf:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <cstdlib>
  3. #include <iostream>
  4.  
  5. using namespace std;
  6.  
  7. int main(int argc, char *argv[])
  8. {
  9.     FILE *ap = fopen("data.txt","r");
  10.     while( !feof(ap) )
  11.     {
  12.            int key1,key2,key3,key4,key5;
  13.            int trash[10];
  14.            char line[128];
  15.            fgets(line,128,ap);
  16.            sscanf(line,"(%d,%d) %d/%d/%d %d:%d {%d %d %d} %d %d %d %d %d",
  17.              &key1,&key2,&trash[0], &trash[1], &trash[2],&trash[3], &trash[4],&key3,&key4,&key5,&trash[5], &trash[6], &trash[7],&trash[8], &trash[9] );
  18.            if( key1 == 1 && key2 == 65 && key3 ==29 && key4 == 10 && key5 == 1 )
  19.            {
  20.                printf("%s",line);
  21.            }
  22.     }
  23.     system("PAUSE");
  24.     return EXIT_SUCCESS;
  25. }
  26.  
  27.  
The hard way would using the ugly string class and use it to parse each line foe the ASCII file, get the fields you want, compare them with the specific fields you expect and save the line if you find a match.
Nov 20 '07 #8
Thanks... So far all is working fine... :))
Now... If I have few ASCII files in directory... Let's say, abc.ASCII, def.ASCII and ghi.ASCII and I need to read all of the files and print this line... First I read abc.ASCII and write line to some txt file, then def.ASCII file and write line into txt file and so one... How can I tell C++ that it reads all ASCII files in some directory?
Nov 21 '07 #9
AHMEDYO
112 100+
Thanks... So far all is working fine... :))
Now... If I have few ASCII files in directory... Let's say, abc.ASCII, def.ASCII and ghi.ASCII and I need to read all of the files and print this line... First I read abc.ASCII and write line to some txt file, then def.ASCII file and write line into txt file and so one... How can I tell C++ that it reads all ASCII files in some directory?
you dont know exactly what is the names of files??, or always you have new files added by new names???,

Kind Regards
Nov 21 '07 #10
you dont know exactly what is the names of files??, or always you have new files added by new names???,

Kind Regards
Hey...
Each hour the new file has been added with different name...

Example:

BR80_29.20071118230000.20071119000000-000.ASCII at 00:00
BR80_29.20071119000000.20071119010000-000.ASCII at 01:00
BR80_29.20071119010000.20071119020000-000.ASCII at 02:00

and so one......

I have directory where are files for 24 hours and I need to process all these files... For example, I want to process all files from yesterday...
Nov 21 '07 #11

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Majordomo | last post: by
17 posts views Thread by Guyon Morée | last post: by
1 post views Thread by Magix | last post: by
4 posts views Thread by Martin Hvidberg | last post: by
2 posts views Thread by Stan Sainte-Rose | last post: by
7 posts views Thread by sweetpotatop | last post: by
6 posts views Thread by Thomas Kowalski | last post: by
9 posts views Thread by =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post: by
5 posts views Thread by dm3281 | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.