LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-01-2007, 03:34 PM   #1
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Rep: Reputation: 37
problem with C (printing out to text file)


Hey there

I recently compiled successfully a C program and the fact is that the program seems to work correctly. The program deals a lot with passing parameters from a text file, processing the data, outputing to the screen some information and finally prints out to a text file the output.
The fact is that everything seems to work well but when outputting to the text file i get as an output
Quote:
%.3f
instead of a float number.
There should be nothing wrong with the code of the program because i know people who they have used it without troubles.
Help from someone familiar would be grateful.

Thanks for your time
 
Old 02-01-2007, 04:57 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
I guess that's the format string you use to output the data? Try %0.3f etc.
 
Old 02-01-2007, 08:29 PM   #3
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Yeah, it sounds like your format specifier string isn't correct. You need to post the line(s) of code that write to the file to be sure though.
 
Old 02-01-2007, 10:07 PM   #4
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Quote:
void writeoutputdata(p, out)
struct network *p;
FILE *out;
{
int i,j;

/* For each input sample, print the sample, followed by the output */
for( i=0; i<p->num_inputs; i++)
{ for( j=0; j<p->io_nodes[0]; j++)
fprintf(out, "%lf ", p->inputs[i].input[j]);
fprintf(out, " ");
for( j=0; j<p->io_nodes[1]; j++)
fprintf(out, "%l0.3f ", p->inputs[i].output[j]);
fprintf(out, "\n");
}
}
This is the writeoutputdata() that program uses. I also tried as you can see l0.3f but it didnt work
To be honest I am not really familiar with formats in C so any help will be really precious.

Thanks for your time.
 
Old 02-04-2007, 01:32 AM   #5
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
Code:
void writeoutputdata(p, out)
struct network *p;
FILE *out;
{
int i,j;

/* For each input sample, print the sample, followed by the output */
for( i=0; i<p->num_inputs; i++)
{ for( j=0; j<p->io_nodes[0]; j++)
fprintf(out, "%lf ", p->inputs[i].input[j]);
fprintf(out, " ");
for( j=0; j<p->io_nodes[1]; j++)
fprintf(out, "%0.3lf ", p->inputs[i].output[j]);
fprintf(out, "\n");
}
}
"%l0.3f" is incorrect as it is a string
it should be "%0.3lf"

are u sure that u donot want any thing to be printed before decimal, if not give some number before '.' like "%5.3lf"
 
Old 02-04-2007, 06:09 PM   #6
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
Dear varun_shrivastava
Thanks very much for your reply.
I believe that you could help me. To be honest I am not really familiar with text formats in C. The fact is that the program outputs with big decimals like 0.321343239 and it is a constraint for me as soon as lets say that i want to use some large piece of data. In such cases i get a segmentation fault message. I would really like from you if you could tell me a way that i could print out double numbers with not large floating points. According to the previous given number something suitable for me should be 0.32

Thanks really much for your time
 
Old 02-05-2007, 01:34 AM   #7
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
Quote:
Originally Posted by manolakis
it is a constraint for me as soon as lets say that i want to use some large piece of data. In such cases i get a segmentation fault message.

i m unable to understand what u want to say in the above line quoted
 
Old 02-05-2007, 02:23 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you won't ever get a seg fault because your float has too many significant digits to printf.


you should post the problem,
not how you think it should be done.
 
Old 02-05-2007, 03:38 PM   #9
manolakis
Member
 
Registered: Nov 2006
Distribution: xubuntu
Posts: 464

Original Poster
Rep: Reputation: 37
I am really sorry but I just tried to describe my case.
Probably you are right but i am not really expert with C and because i tried many times to make that program work but without any success i thought that someone could help me.
Any ideas now how can i print out a number like 0.321343239 with less signficant bits, like as i said 0.32?
If someone knows Java and C i would have been really obliged to him if he could show me a way to do it


Thanks again for your time and for sharing your knowledge.
 
Old 02-05-2007, 07:06 PM   #10
wuqijia
LQ Newbie
 
Registered: Jan 2007
Posts: 1

Rep: Reputation: 0
try %.3Lf,
I think float should be formatted with "L" instead of 'l'.
 
Old 02-06-2007, 02:22 AM   #11
varun_shrivastava
Member
 
Registered: Jun 2006
Distribution: Ubuntu 7.04 Feisty
Posts: 79

Rep: Reputation: 15
if u write "%10.5f" it means
total of 10 characters will be printed with 5 decimal digits and they will be right justified

whereas "%-10.5f" will print the same but left justified

so u want to print your output as 0.32
so there are 4 characters and two after decimal : use "%4.2lf"

read c programming language by Denice Ritche : Best book to learn c programming

Last edited by varun_shrivastava; 02-06-2007 at 02:24 AM.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with writing in a text file in C manolakis Programming 5 12-06-2006 10:18 PM
reading and printing out a text file in html mrobertson Programming 1 07-08-2005 11:47 PM
Printing numbers from a text file dynamically mrobertson Programming 1 06-28-2005 08:19 AM
Problem printing a few characters in a PDF File provo1234 Linux - General 1 04-25-2005 08:08 AM
Printing webpages and emails problem - text doesn't fit and overwrites itself silverbirch Linux - Newbie 1 02-28-2005 07:14 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration