473,326 Members | 2,126 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,326 software developers and data experts.

Help on File Reading in C programming.

i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way
r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after
one instance OR please let me know what i have to do to move the file
pointer to next line, after reading 48 in the above example.

please help.
Thanks
AD.

Nov 14 '05 #1
4 5961


Ameya wrote:
i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way
r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after
one instance OR please let me know what i have to do to move the file
pointer to next line, after reading 48 in the above example.


There are many ways to do this. Here are two:

- Use fgets() to read each complete line into an array
of `char', and use sscanf() to extract the interesting
fields from the array.

- Use scanf() or fscanf() to read the interesting fields
from the beginning of each line, then use a loop like

int ch;
while ((ch = getc(stream) != '\n' && ch != EOF)
;

to skip the rest of the line.

--
Er*********@sun.com

Nov 14 '05 #2
Ameya wrote:

i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way

r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after
one instance OR please let me know what i have to do to move the file
pointer to next line, after reading 48 in the above example.


/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

#define ARGV_0 "new"
#define MAX_LENGTH 60
#define str(s) # s
#define xstr(s) str(s)

int get_line(FILE *fd, char *line);
int get_line_up_to_left_bracket(FILE *fd, char *line);

int main(int argc, char *argv[])
{
FILE *fd;
char string[MAX_LENGTH + 1];
int rc;

if (argc > 1) {
fd = fopen(argv[1], "r");
if (fd == NULL) {
fprintf(stderr,
"\nfopen() problem with \"%s\"\n", argv[1]);
exit(EXIT_FAILURE);
}
printf("\n%s opened for reading.\n", argv[1]);
rc = get_line(fd, string);
while (rc == 1) {
puts(string);
rc = get_line_up_to_left_bracket(fd, string);
}
fclose(fd);
printf("\n%s closed.\n", argv[1]);
} else {
puts("Usage:\n >" ARGV_0 " <TRACE_FILE.txt> ...\n\n"
"Example:\nC:\\>" ARGV_0 " trace.txt");
}
return 0;
}

int get_line_up_to_left_bracket(FILE *fd, char *line)
{
int rc;

do {
rc = fscanf(fd, "%" xstr(MAX_LENGTH) "[^[\n]%*[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
} while (rc == 0);
return rc;
}

int get_line(FILE *fd, char *line)
{
int rc;

do {
rc = fscanf(fd, "%" xstr(MAX_LENGTH) "[^\n]%*[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
} while (rc == 0);
return rc;
}

/* END new.c */

--
pete
Nov 14 '05 #3
Thanks for the help guys !!
Will try it out !

Eric Sosman wrote:
Ameya wrote:
i want to read from a trace file ....but my problem is i am interested only in first 6 columns of my 10 - 12 wide text file. Each column is separated by a space (the number of columns vary on each row) for. e.g an entry looks this way
r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for the first line, but after processing first line, i want to scan from next line ignoring the data from square brackets.

is there any other function which moves the pointer to next line after one instance OR please let me know what i have to do to move the file pointer to next line, after reading 48 in the above example.


There are many ways to do this. Here are two:

- Use fgets() to read each complete line into an array
of `char', and use sscanf() to extract the interesting
fields from the array.

- Use scanf() or fscanf() to read the interesting fields
from the beginning of each line, then use a loop like

int ch;
while ((ch = getc(stream) != '\n' && ch != EOF)
;

to skip the rest of the line.

--
Er*********@sun.com


Nov 14 '05 #4

"Ameya" <da*****@gmail.com> wrote in message
news:11*********************@g43g2000cwa.googlegro ups.com...
i want to read from a trace file ....but my problem is i am interested
only in first 6 columns of my 10 - 12 wide text file. Each column is
separated by a space (the number of columns vary on each row) for. e.g
an entry looks this way
r 10.33011 _0_ AGT 0 ACK 48 [0:234 0:44 8 0] [0 0] [5 0]

i only want to read till 48. I am able to read all the variables for
the first line, but after processing first line, i want to scan from
next line ignoring the data from square brackets.


The solution is called Perl. It is a language that pulls data from text
files. It has many, many functions to extract text and transform it.

--
Mabden
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

45
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
1
by: AKF | last post by:
I am trying to use data structures to open a file (void display_Stock_Item(Stock_Item si);) from a text file. I also need to be able to write to the file and also to delete from the file using...
3
by: James Tunic | last post by:
I am trying to write a program (in C) for reading grid1.dat and grid2.dat and displaying the data it has read on the screen. I have got as far as the program listed below, it seems to compile...
1
by: john_g83 | last post by:
Hello all, new user to c programming and am having difficulty with a little program I am trying to write. Basically what I have is a file which contains a list of words i.e: tst.txt: customer...
3
by: Chi | last post by:
what is the "unable to write data to the transport connection" I use the oreilly , programming c# using System; using System.Net.Sockets; using System.Text; using System.IO; // get a file...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
22
by: Amali | last post by:
I'm newdie in c programming. this is my first project in programming. I have to write a program for a airline reservation. this is what i have done yet. but when it runs it shows the number of...
7
by: ianenis.tiryaki | last post by:
well i got this assignment which i dont even have a clue what i am supposed to do. it is about reading me data from the file and load them into a parallel array here is the question: Step (1) ...
30
by: carlos123 | last post by:
Ok I am working on a Hall Pass program for my computer programming class. There are 3 things that I am confused on. 1. Reading a file. 2. Taking that data read from the file and putting it into...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.