Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > fscanf reading lines

Reply
Thread Tools

fscanf reading lines

 
 
bhanuprakash@gmail.com
Guest
Posts: n/a
 
      03-08-2006
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.

 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      03-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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)
 
Reply With Quote
 
 
 
 
joelperr@gmail.com
Guest
Posts: n/a
 
      03-08-2006
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.

 
Reply With Quote
 
bhanuprakash@gmail.com
Guest
Posts: n/a
 
      03-08-2006
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.

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      03-08-2006
(E-Mail Removed) 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) (E-Mail Removed) <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.
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-08-2006
(E-Mail Removed) 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
 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      03-08-2006
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
 
Reply With Quote
 
Fred Kleinschmidt
Guest
Posts: n/a
 
      03-08-2006

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
no error by fscanf on reading from output file V.Subramanian, India C Programming 18 10-31-2011 08:46 PM
can fscanf skip reading data conditionally? John C Programming 4 09-27-2006 08:07 PM
Missing Lines With fscanf() Psibur C Programming 4 06-11-2004 09:00 PM
fscanf to read lines from file? CJ C Programming 2 05-21-2004 11:51 AM
Problems when reading from a file with fscanf Benedicte C Programming 3 02-04-2004 02:57 PM



Advertisments