Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 02-08-2005, 05:48 PM   #1
Registered: May 2004
Distribution: red hat 9
Posts: 56

Rep: Reputation: 15
binary file problem

okay this is a piece of code....

int fdf;
int rcf;

genespect is pointer to a double array
it was declared as

double *genespect;


after the execution i see that the file output7 has been probably has
legit data too...i cant open it....gedit does not open it .....

i basically want this to be an ascii file with one number per line...
how do i do that?
i know it isnt like that cos' gnuplot isnt able to plot it(it requires the file to be in one number per line format)

Old 02-08-2005, 06:01 PM   #2
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
You're best off using stdio functions:
fp = fopen("output7", "w");
for(i = 0;i < len;++i)
  fprintf(fp, "%f\n", genespect[i]);
"binary files" and "one number per line" don't mix. Binary files have no concept of newlines so they also don't have any concept of lines.
Old 02-08-2005, 06:11 PM   #3
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 110Reputation: 110
itsme86 already told, but I'd already made up an example. I'll post it anyway.
#include <stdio.h>
#include <stdlib.h>

int main()
    int i, len;
    double *genespect;
    FILE *file;

    len = 2;
    genespect = calloc(len, sizeof(double));
    if (genespect == NULL) {
        fprintf(stderr, "Error: Ouot of memory\n");
        return 1;
    /* Just two values here */
    genespect[0] = 22.0/7.0;
    genespect[1] = 355.0/113.0;

    /* Open file */
    /* Using higher level stdio here */
    file = fopen("output.txt", "w"); 
    if (file ==NULL) {
        perror("Opening output file.");
        return 1;

    for (i = 0; i < len ; ++i) {
        /* Using stdio calls you can do convenient
         * printf-conversion while writing to a file.
        fprintf(file, "Value #%d: %f\n", i, genespect[i]);
    return 0;
Old 02-08-2005, 06:33 PM   #4
Registered: May 2004
Distribution: red hat 9
Posts: 56

Original Poster
Rep: Reputation: 15
Seriously man,you guys are lifesavers!!!!!!
it worked!!


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Automatically tell text file from binary file? Hosiah Linux - Software 3 05-31-2005 05:22 AM
problem in reading Microsoft word as a binary file ljqu_happy Programming 15 02-02-2005 10:10 AM
convert text file to binary excel file ust Linux - General 2 11-23-2004 02:33 AM
Problem running execute binary file zekko Linux - General 18 08-08-2003 06:14 PM
binary file problem lf412 Linux - General 3 02-05-2003 02:18 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 PM.

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