Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > File handling code

Reply
Thread Tools

File handling code

 
 
neha_chhatre@yahoo.co.in
Guest
Posts: n/a
 
      02-14-2008
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
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-14-2008
In article <(E-Mail Removed)>,
<(E-Mail Removed)> wrote:
>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
--
"The human mind is so strangely capricious, that, when freed from
the pressure of real misery, it becomes open and sensitive to the
ideal apprehension of ideal calamities." -- Sir Walter Scott
 
Reply With Quote
 
 
 
 
santosh
Guest
Posts: n/a
 
      02-14-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

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

 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      02-14-2008
(E-Mail Removed) writes:
[...]
> while(!feof(fp))

[...]

Please read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) <(E-Mail Removed)>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bartc
Guest
Posts: n/a
 
      02-14-2008

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

--
Bart



 
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
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
python list handling and Lisp list handling Mark Tarver Python 22 04-26-2009 09:36 PM
Is faster handling hexadecimal values than handling chars? IƱaki Baz Castillo Ruby 1 04-15-2008 09:04 AM
what is the difference between code inside a <script> tag and code in the code-behind file? keithb ASP .Net 1 03-29-2006 01:00 AM
Repost:Handling large C code (code in general) teachtiro@yahoo.com C Programming 1 04-19-2005 03:28 PM



Advertisments