473,507 Members | 2,504 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File handling code

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
Feb 14 '08 #1
3 2369
In article <7a**********************************@e6g2000prf.g ooglegroups.com>,
<ne**********@yahoo.co.inwrote:
>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
Feb 14 '08 #2
ne**********@yahoo.co.in writes:
[...]
while(!feof(fp))
[...]

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

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 14 '08 #3

<ne**********@yahoo.co.inwrote in message
news:7a**********************************@e6g2000p rf.googlegroups.com...
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

Feb 14 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3188
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is...
1
3247
by: Sean W. Quinn | last post by:
Hey folks, I have a question regarding file handling, and the preservation of class structure. I have a class (and I will post snippets of code later in the post) with both primitive data...
5
2277
by: John Douglass | last post by:
I'm fairly new to doing involved file i/o and I came across something weird with a program I'm writing (modifying MIDI data, if it makes any difference). With some input files, when I modify data...
8
26701
by: Gabe Moothart | last post by:
Hi, I'm writing a windows service which interacts with a separate process. Basically, it calls a process which creates a file, and then my service reads that file. The problem is, the external...
14
3861
by: Al Smith | last post by:
I need help in implementing proper error handling. I am trying to upload a file based on the sample code below. The code works well except if the file selected is too big. I do know about the...
3
4915
by: J055 | last post by:
Hi How do I tell the user he has tried to upload a file which is too big... 1. when the httpRuntime.maxRequestLength has been exceeded and 2. when the uploaded file is under then...
1
6441
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
19
4753
by: rmr531 | last post by:
First of all I am very new to c++ so please bear with me. I am trying to create a program that keeps an inventory of items. I am trying to use a struct to store a product name, purchase price,...
5
64581
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
5
6791
by: kailashchandra | last post by:
I am trying to upload a file in php,but it gives me error msg please Help me? My Code is like below:- i have one php file named upload.php and i have another html file named upload.html and...
0
7223
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
7314
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,...
1
7030
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1540
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
758
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.