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...!!!
10 7341
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
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.
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...
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
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...
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 - 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
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: -
-
#include <cstdlib>
-
#include <iostream>
-
-
using namespace std;
-
-
int main(int argc, char *argv[])
-
{
-
FILE *ap = fopen("data.txt","r");
-
while( !feof(ap) )
-
{
-
int key1,key2,key3,key4,key5;
-
int trash[10];
-
char line[128];
-
fgets(line,128,ap);
-
sscanf(line,"(%d,%d) %d/%d/%d %d:%d {%d %d %d} %d %d %d %d %d",
-
&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] );
-
if( key1 == 1 && key2 == 65 && key3 ==29 && key4 == 10 && key5 == 1 )
-
{
-
printf("%s",line);
-
}
-
}
-
system("PAUSE");
-
return EXIT_SUCCESS;
-
}
-
-
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.
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?
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
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...
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
| | | | | | | | | | |