Thread: Convert array of characters in file to integers

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    12

    Convert array of characters in file to integers

    I have a text file consisting of 0's and 1's. I want to read these characters into an array as integers and then save that array into another file.

    I'm using fread to read the data into the array. I'm using fprintf to write the data to the new file.

    If I declare the array as an int then nothing gets saved to the file.

    When I declare the array as a char, the 0's and 1's are being saved as their ascii equivalents (48 and 49).

    How can I save these characters as integers to the file? Do I need to somehow convert the array of characters to an array of integers?

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    There is no such need. Save them in a char array. A text file has characters not integers. Each character is 1 byte. An integer is 4 bytes. No need to gets confused.

    There is no such thing as saving integers to a text file.
    You can do that in a binary file. But what are your specifications?
    You want each 0 and 1 to be one byte (a character) or just 1 bit?

    Also, you can always substract -49 from the characters and use a char array as an array of integers.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    You can write ints directly to a file, but then that file will not be a text file, and you will not be able to meaningfully read that file as text. This is called serialization.
    Code:
    #include <stdio.h>
    
    int main(void) {
    	FILE *fp = fopen("test.data","wb");
    	int nums[] = { 1, 0, 666, -2436523 }, copy[4], i;
    	
    	fwrite(nums,sizeof(int),4,fp);
    	fclose(fp);
    	
            fp=fopen("test.ints","rb");
    	fread(copy,sizeof(int),4,fp);
    	fclose(fp);
    	for(i=0;i<4;i++) printf("%d\n",copy[i]);
    	
    	return 0;
    }
    Notice that fwrite and fread use "wb" and "rb" for write/read binary. This is important.

    The reason you cannot read the file as text is that text is a series of one byte char ascii values. An int is a four byte "little endian" value.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by MK27 View Post
    An int is a four byte "little endian" value.
    That is specific to the micro - which can be "big" or "little" endian.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Be specific about what you want to do with the characters in the input text file.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-16-2009, 06:00 AM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Text file to 2D Array PLEASE HELP!
    By lostboy101 in forum C Programming
    Replies: 0
    Last Post: 03-26-2002, 10:51 AM