473,666 Members | 2,010 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.t xt","rb");

while(!feof(fp) )
{

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

please send reply as soon as possible its urgent
Feb 14 '08 #1
3 2380
In article <7a************ *************** *******@e6g2000 prf.googlegroup s.com>,
<ne**********@y ahoo.co.inwrote :
>hey please help me execute the code on ubuntu in C
>#include<stdio .h>
#include<stdli b.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.t xt","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",tic ktock);
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**********@ya hoo.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_Keit h) <ks***@mib.or g>
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**********@y ahoo.co.inwrote in message
news:7a******** *************** ***********@e6g 2000prf.googleg roups.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.t xt","rb");

while(!feof(fp) )
{

sscanf(fp, "time=%f value=%d", &ticktock, &val);
printf("%f",tic ktock);
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.t xt","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
3198
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 indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"): handle_line(line)
1
3261
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 structures (ints), and more complex data structures (strings and vectors) in it, and would like to write the entire class to a data file that could then be read back and loaded. However I'm having difficulty with this -- I found out (due to an...
5
2282
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 and then output to a new file, the program will crash. The catch is that when I open up the output file to see what the program managed to write, it always makes it to an offset of xFFF. For example, with one file it crashed at 2FFF, another at...
8
26708
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 process can take a second or two to finish writing the file. If I try to read the file to soon, I get an exception that "The process cannot access the file because it is being used by another process". I could just set a timer, but the time it...
14
3882
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 maxRequestLength parameter of the <httpRuntime> section and that works as expected. What I want is to enforce a max file size but haven't been able to trap the error thrown when the file is too large and that's where I could use some help.
3
4922
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 httpRuntime.maxRequestLength For point 1. it would be good to at least display a nice error page. IE seems to just display a blank page.
1
6477
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" setting to be "E_ALL", notices are still not getting reported. The perms on my file are 664, with owner root and group root. The php.ini file is located at /usr/local/lib/php/php.ini. Any ideas why the setting does not seem to be having an effect? ...
19
4769
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, sell price, and a taxable flag (a Y/N char) and then write this all out to a file (preferably just a plain old text file) and then read it in later so that I can keep a running inventory. The problem that I am running into is when I write to the...
5
64622
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 this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts, disregarding the syntax that stream contains. Parsing is actually including the syntax in order to make...
5
6803
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 inside html i am calling php file upload.php:- <?php
0
8445
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8551
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7386
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6198
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1776
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.