File handling code

N

neha_chhatre

hey please help me execute the code on ubuntu in C

#include<stdio.h>
#include<stdlib.h>

int main()
{
FILE *fp;
char ch;
float ticktock;
int val;
fp=fopen("pt1.txt","rb");

while(!feof(fp))
{

sscanf(fp, "time=%f value=%d", &ticktock, &val);
printf("%f",ticktock);
printf("%d",val);


}


}

please send reply as soon as possible its urgent
 
W

Walter Roberson

hey please help me execute the code on ubuntu in C
#include<stdio.h>
#include<stdlib.h>

int main()

Usually you would use int main(void)
{
FILE *fp;
char ch;

You do not appear to use 'ch', so you might as well get rid of it.
float ticktock;
int val;
fp=fopen("pt1.txt","rb");

You fail to check to see whether the open succeeded by checking
the value of fp after the fopen().
while(!feof(fp))

feof() in C never makes a -prediction- about whether the next read
would work or not (the fact that no data is available in the buffer
right now does not mean that end of file has been reached -- you
might be connected to a network socket or the action of asking for
more data might cause more data to be released to you.) Therefore
testing for feof() first before the read does not work:
instead you need to check the return value of each file read
to see whether you got the data you wanted.
{

sscanf(fp, "time=%f value=%d", &ticktock, &val);
printf("%f",ticktock);
printf("%d",val);

Typically if you have two successive printf()'s, you would merge
them into a single call, such as
printf("%f%d, ticktock, val);

Oh dear, you did not include any spacing between the output
values and you do not include and line termination characters.
Everything is going to be output as a single long string of
digits (with some decimal points in places.)


You declared main as returning an int but you failed to return
any value. That gives undefined behaviour for C90 (but will work
in C99.)
please send reply as soon as possible its urgent

http://www.officeplayground.com/lackplanning.html
 
S

santosh

hey please help me execute the code on ubuntu in C

#include<stdio.h>
#include<stdlib.h>

Why don't you use space for readability?
int main()
{
FILE *fp;
char ch;
float ticktock;
int val;
fp=fopen("pt1.txt","rb");

Do check all library calls. A file open fail for many reasons.
while(!feof(fp))

This is wrong. In C you use feof (and ferror) *after* an I/O function
call has returned whatever value it uses to signal failure.

For example getc returns EOF on either end-of-file or an unspecified
error. You can call feof and ferror *after* a getc call has returned
EOF to find out which condition is true.
{

sscanf(fp, "time=%f value=%d", &ticktock, &val);

sscanf reads from a string in memory. You probably want fscanf.
Also check all these calls for failure. Failing to do so and proceeding,
assuming success, can cause hard to trace problems.
printf("%f",ticktock);
printf("%d",val);

Some spacing between these outputs would help readability.

You should have a return statement here.

Use the -W, -Wall, -ansi and -pedantic flags with gcc to get it to
report potential problems that it might otherwise keep silent about.
}

please send reply as soon as possible its urgent

See:

<http://www.c-faq.com/>
 
B

Bartc

hey please help me execute the code on ubuntu in C

#include<stdio.h>
#include<stdlib.h>

int main()
{
FILE *fp;
char ch;
float ticktock;
int val;
fp=fopen("pt1.txt","rb");

while(!feof(fp))
{

sscanf(fp, "time=%f value=%d", &ticktock, &val);
printf("%f",ticktock);
printf("%d",val);
}
}

You want to read in a text file and print a summary of the contents to the
screen?

What is the exact format of the input file, something like this:

time=1234.56 value=7654
time=8329.01 value=9127
....
until end of file, with each pair on a new line?

And you want to print to the screen the following:

1234.56 7654
8329.01 9127
....?

(This sounds odd, it's makes more sense for the bare numbers to be in the
file and the labels to be added to screen output.)

What do you want to do about blank lines in the input, and incorrect
formats? It's a good idea to read the actual "time" and "value" labels and
check these are as expected, as sometimes things can get out of step.

Sorry can't offer code ideas (file handling is a pig in C with the
counter-intuitive feof() and inability to properly read a complete line of
input using fgets()). But it's sometimes useful to fully spell out your
task.

But, if all you really want to do is to copy the file unchanged to the
screen, then this is fairly easy (you don't even need a program really):

int c;

fp=fopen("pt1.txt","rb");

if (fp!=0)
{while (1)
{c=fgetc(fp);
if (c==EOF) break;
printf("%c",c);
};
fclose(fp);
}
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top