473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 7643
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,208 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

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

Similar topics

0
by: Majordomo | last post by:
-- >>>> --36742377 **** Command '--36742377' not recognized. >>>> Content-Type: text/plain; charset=us-ascii **** Command 'content-type:' not recognized. >>>> Content-Transfer-Encoding: 7bit...
17
by: Guyon Morée | last post by:
what is the difference? if I open a text file in binary (rb) mode, it doesn't matter... the read() output is the same.
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
4
by: Martin Hvidberg | last post by:
Dear group I need to make a very simple piece of code in C, that can be command line executed and will compile on Linux, i.e. gcc. It should read a ascii Comma Separated Values (CSV) file and...
2
by: Stan Sainte-Rose | last post by:
Hi, I need to read a kind of text file from vb.net I do know how to read a simple file, but this one seems to have special characters. Some characters appears like squares when open this file...
7
by: sweetpotatop | last post by:
Hello, I have a txt file which has a few lines of special characters: This is A line from the text file: ...
6
by: Thomas Kowalski | last post by:
Hi, currently I am reading a huge (about 10-100 MB) text-file line by line using fstreams and getline. I wonder whether there is a faster way to read a file line by line (with std::string line)....
9
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= | last post by:
I want to open a text file and format it into a specific line and then apply color to a specific location of the text and then display it in a RichTextBox after all of this is done. I can do all...
5
by: dm3281 | last post by:
Hello, I have a text report from a mainframe that I need to parse. The report has about a 2580 byte header that contains binary information (garbage for the most part); although there are a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.