471,584 Members | 1,598 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

fscanf reading lines

I am trying to use fscanf to read my test file. In my test file i
sometimes have blank lines.
When I try to read using the following format.

fscanf(fp,"%[^\n]\n",temp_str);

If there is any blank line it reads contents from the next line. How do
I read blank into my variable if the line is blank.

Example

Line Number Input
1 Hi
2
3 Smile

In the above scenario if I try to read line number 2 where it is blank
it reads "Smile" into my temp_sttring variable. How do I ensure if
there is a blank line my variable also ends up blank.

Mar 8 '06 #1
7 27282
bh**********@gmail.com said:
When I try to read using the following format.

fscanf(fp,"%[^\n]\n",temp_str);

If there is any blank line [fscanf] reads contents from the next line.
How do I read blank into my variable if the line is blank.


Easy. Don't use fscanf.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Mar 8 '06 #2
Hello,

when you are using fscanf(fp,"%[^\n]\n",temp_str); you are saying that
the token you wish to not parse is '\n'. Since the blank line is
uniquely composed of '\n', then it skips that line and goes to the
next. Check the return value of fscanf to see how many values were
read, which will tell you if there was a blank line or not.
Joel P.

Mar 8 '06 #3
Hi,
what would be the best possible solution in this scenario. i
tried using fgets but it has another problem.
if I want to read something like
Line No Input
1 SMILE

fgets(temp_str,sizeof(temp_str),fp);

Then in my variable temp_str it contains the value as "SMILE
".

There are some additional characters appended to my variable.

I want only the string "SMILE" come into the variable temp-str.

Please help me on this.

Mar 8 '06 #4
bh**********@gmail.com writes:
what would be the best possible solution in this scenario. i
tried using fgets but it has another problem.
if I want to read something like
Line No Input
1 SMILE

fgets(temp_str,sizeof(temp_str),fp);

Then in my variable temp_str it contains the value as "SMILE
".

There are some additional characters appended to my variable.

I want only the string "SMILE" come into the variable temp-str.


You'll have to be much clearer about what you're doing.

Are you saying that line 1 of your input file contains only the string
"SMILE" (followed by a new-line) and nothing else?

Show us a complete compilable program that illustrates the problem
you're having.

(And read <http://cfaj.freeshell.org/google/> *before* you post
another followup.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Mar 8 '06 #5
bh**********@gmail.com wrote:

I am trying to use fscanf to read my test file. In my test file i
sometimes have blank lines.
When I try to read using the following format.

fscanf(fp,"%[^\n]\n",temp_str);

If there is any blank line it reads contents from the next line.
How do
I read blank into my variable if the line is blank.
rc = fscanf(fp, "%[^\n]", temp_str);
if (!feof(fp)) {
getc(fp);
}
if (rc == 0) {
*line = '\0';
}
Example

Line Number Input
1 Hi
2
3 Smile

In the above scenario if I try to read line number 2 where it is blank
it reads "Smile" into my temp_sttring variable. How do I ensure if
there is a blank line my variable also ends up blank.


/* BEGIN type_.c */
/*
** This program uses fscanf
** to read lines of text files.
*/
#include <stdio.h>
#include <stdlib.h>

#define ARGV_0 type_
#define str(s) # s
#define xstr(s) str(s)

unsigned long max_line_len(FILE *fd);

int main(int argc, char *argv[])
{
int rc;
FILE *fd;
char *line;
unsigned long length;
unsigned long line_length;

if (argc > 1) {
line_length = 1;
line = malloc(line_length + 1);
if (line == NULL) {
fprintf(stderr, "line_length is %lu\n", line_length);
exit(EXIT_FAILURE);
}
while (*++argv != NULL) {
fd = fopen(*argv, "r");
if (fd != NULL) {
length = max_line_len(fd);
if (length > line_length) {
line_length = length;
free(line);
line = malloc(line_length + 1);
if (line == NULL) {
fprintf(stderr,
"line_length is %lu\n", line_length);
exit(EXIT_FAILURE);
}
}
do {
rc = fscanf(fd, "%[^\n]", line);
if (!feof(fd)) {
getc(fd);
}
switch (rc) {
case 0:
*line = '\0';
case 1:
puts(line);
default:
break;
}
} while (rc != EOF);
fclose(fd);
} else {
fprintf(stderr,
"\nfopen() problem with \"%s\"\n", *argv);
break;
}
}
free(line);
} else {
puts(
"Usage:\n>" xstr(ARGV_0)
" <FILE_0.txt> <FILE_1.txt> <FILE_2.txt> ...\n"
);
}
return 0;
}

unsigned long max_line_len(FILE *fd)
{
unsigned long count, max;
int rc;

count = max = 0;
rc = getc(fd);
while (rc != EOF) {
if (rc == '\n') {
if (count > max) {
max = count;
}
count = 0;
} else {
++count;
}
rc = getc(fd);
}
rewind(fd);
return max;
}

/* END type_.c */
--
pete
Mar 8 '06 #6
pete wrote:
line = malloc(line_length + 1); rc = fscanf(fd, "%[^\n]", line);
if (!feof(fd)) {
getc(fd);
} unsigned long max_line_len(FILE *fd)
{
rewind(fd);
}


If you don't want to go through the file twice,
and don't mind truncating long lines:

#define LENGTH 80

#define str(x) # x
#define xstr(x) str(x)

line_length = LENGTH;
line = malloc(line_length + 1);

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

--
pete
Mar 8 '06 #7

<bh**********@gmail.com> wrote in message
news:11**********************@j52g2000cwj.googlegr oups.com...
I am trying to use fscanf to read my test file. In my test file i
sometimes have blank lines.
When I try to read using the following format.

fscanf(fp,"%[^\n]\n",temp_str);

If there is any blank line it reads contents from the next line. How do
I read blank into my variable if the line is blank.

Example

Line Number Input
1 Hi
2
3 Smile

In the above scenario if I try to read line number 2 where it is blank
it reads "Smile" into my temp_sttring variable. How do I ensure if
there is a blank line my variable also ends up blank.


If you are trying to read the last blank-delimited string:
1. read the line using fgets
2. (safety check) if last character in buffer is not newline, repeat
3. replace trailing newline with '\0'
4. use ptr=strrchr(text, ' ') to find the last blank
5. if ptr==null the entire string is what you want
6. if ptr != null, ++ptr points to the beginning of the desired string
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project

Mar 8 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Benedicte | last post: by
4 posts views Thread by Psibur | last post: by
7 posts views Thread by Thomas Sourmail | last post: by
7 posts views Thread by Kay | last post: by
1 post views Thread by siliconwafer | last post: by
37 posts views Thread by PeterOut | last post: by
10 posts views Thread by rsk | last post: by
5 posts views Thread by a | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by lumer26 | last post: by

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.