fscanf reading lines

B

bhanuprakash

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.
 
R

Richard Heathfield

(e-mail address 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.
 
J

joelperr

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.
 
B

bhanuprakash

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.
 
K

Keith Thompson

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.)
 
P

pete

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 */
 
P

pete

pete said:
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);
}
 
F

Fred Kleinschmidt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top