LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem with C (printing out to text file) (https://www.linuxquestions.org/questions/programming-9/problem-with-c-printing-out-to-text-file-524774/)

manolakis 02-01-2007 03:34 PM

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

Mara 02-01-2007 04:57 PM

I guess that's the format string you use to output the data? Try %0.3f etc.

Dark_Helmet 02-01-2007 08:29 PM

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.

manolakis 02-01-2007 10:07 PM

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.

varun_shrivastava 02-04-2007 01:32 AM

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"

manolakis 02-04-2007 06:09 PM

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

varun_shrivastava 02-05-2007 01:34 AM

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

bigearsbilly 02-05-2007 02:23 AM

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.

manolakis 02-05-2007 03:38 PM

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.

wuqijia 02-05-2007 07:06 PM

try %.3Lf,
I think float should be formatted with "L" instead of 'l'.

varun_shrivastava 02-06-2007 02:22 AM

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


All times are GMT -5. The time now is 12:45 PM.