LinuxQuestions.org
Help answer threads with 0 replies.
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 06-10-2006, 11:22 AM   #1
cdog
Member
 
Registered: Dec 2005
Posts: 65

Rep: Reputation: 15
Reading text file-writting binary file


Hi guys. I just need some advice here.
I have a text file ( containg strings and numbers)
I read the file ("r") and every string and number should be saved in a binary file. After that I would like to read back the binary file and save it all in another text file.
I'm working in C.
What I did so far is get the strings and numbers (one by one) and use - for strings - fwrite(string,sizeof(char)*strlen(str),output_file). The string is stored ok (I don't know if this is in binary state or not, but after the string, it adds weird data and the numbers are not stored ok. Any help would apreciated. Thanks
 
Old 06-10-2006, 10:11 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
Use a struct for storing your strings as char[] and numbers as int. Write out the structs to a binary file like this:
Code:
#include <stdio.h>

struct binstr
{
   char bigStr[100];
};

void main()
{
   FILE *f;
   struct binstr BS;
   struct binstr GS;

   f = fopen("binfile", "w" );

   strcpy( BS.bigStr,  "Hello There" );

   fwrite( &BS, sizeof(struct binstr), 1, f );

   fclose( f );

   f = fopen("binfile", "r" );
   fread( &GS, sizeof( struct binstr ), 1, f );
   printf("Str is: %s\n", GS.bigStr );

   fclose( f );

}
 
Old 06-11-2006, 05:10 PM   #3
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Thanks crabboy, I managed to write it myself.
 
Old 06-13-2006, 06:08 AM   #4
cdog
Member
 
Registered: Dec 2005
Posts: 65

Original Poster
Rep: Reputation: 15
Ok, I managed to fvck this up. The struct contains some integers and few strings of unknow size. Is there an easy way to to write this struct to the binary file?
Thanks
 
Old 06-13-2006, 08:47 AM   #5
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,821

Rep: Reputation: 121Reputation: 121
The strings need to be a fixed size.
 
Old 06-13-2006, 11:56 AM   #6
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
this might be helpful
this is basically the same as cat-ing everything under ascii 127:
Code:
#include "stdio.h"

main(int argc, char *argv[])
{
 int c;
 FILE * fstream;

 fstream = fopen(argv[1], "r");
 c = fgetc(fstream);

 while(c != EOF)
 {
  if(c >= 0 && c <= 127)
   printf("%c", c);
  c = fgetc(fstream);
 }
 fclose(fstream);
}
feel free to modify to your heart's content
 
  


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
Reading and Writing integers to binary file oulevon Programming 2 02-26-2006 12:27 AM
What 's the different between text file and binary file? taureanyang General 3 07-25-2005 08:37 AM
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

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

All times are GMT -5. The time now is 11:35 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