472,780 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,780 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 7603
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
How does React native implement an English player?
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.