LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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-08-2005, 06:48 PM   #1
vrdhananjay
Member
 
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;
fdf=open("output7",O_WRONLY|O_CREAT|O_TRUNC,0666);
rcf=write(fdf,genespect,len);
close(fdf);

genespect is pointer to a double array
it was declared as

double *genespect;

genespect=(double*)malloc(len*sizeof(double));

after the execution i see that the file output7 has been made.it 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)

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

Rep: Reputation: 58
You're best off using stdio functions:
Code:
fp = fopen("output7", "w");
for(i = 0;i < len;++i)
  fprintf(fp, "%f\n", genespect[i]);
fclose(fp);
"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, 07:11 PM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
itsme86 already told, but I'd already made up an example. I'll post it anyway.
Code:
#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]);
    }
    fclose(file);
    return 0;
}
 
Old 02-08-2005, 07:33 PM   #4
vrdhananjay
Member
 
Registered: May 2004
Distribution: red hat 9
Posts: 56

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


Reply


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 06:22 AM
problem in reading Microsoft word as a binary file ljqu_happy Programming 15 02-02-2005 11:10 AM
convert text file to binary excel file ust Linux - General 2 11-23-2004 03:33 AM
Problem running execute binary file zekko Linux - General 18 08-08-2003 07:14 PM
binary file problem lf412 Linux - General 3 02-05-2003 03:18 PM


All times are GMT -5. The time now is 07:06 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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration