By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,182 Members | 1,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,182 IT Pros & Developers. It's quick & easy.

Help on File Reading in C programming.

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a


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

P: n/a
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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.