473,386 Members | 1,943 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,386 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 27693
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Benedicte | last post by:
Hi, I'm getting some problems when using fscanf to read a file. This is a piece of the program code: main () { /*** Variable declaration ***/ FILE *vpfile; /*** Data file ***/
4
by: Psibur | last post by:
Hello, trying to get back into c and was having issue with reading a simple text file with an aribtrary # of lines with 3 int's per line, with the eventual purpose of putting each int into an...
7
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
7
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
1
by: siliconwafer | last post by:
Hi All, here is one code: int main() { FILE*fp; unsigned long a; fp = fopen("my_file.txt","w+"); a = 24; fprintf(fp,"%ld",a); while(fscanf(fp,"%ld",&a) == 1) {
4
by: John | last post by:
I need to read data from the file like the following with name and score, but some line may only has name without score: joe 100 amy 80 may Here's my code, but it couldn't read the line with...
37
by: PeterOut | last post by:
I am using MS Visual C++ 6.0 on Windows XP 5.1 (SP2). I am not sure if this is a C, C++ or MS issue but fscanf has been randomly hanging on me. I make the call hundreds, if not thousands, of...
10
by: rsk | last post by:
Hi Friends, I have written a code which suppose to read all the numbers from a hex file,But to my surprise the code is skiping every alternate value.Don't know why? Can you please help me in...
5
by: a | last post by:
After reading FAQ comp.lang.c section 12 and googling again, still there is no threads talking about reading a series of numbers. The input files, somehow structured, is exemplified below: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.