Create file with fixed length and initiate it to all 0's

This is a discussion on Create file with fixed length and initiate it to all 0's within the C Programming forums, part of the General Programming Boards category; Hi all, Is there a good way to create a new file with 1024 bytes, and initiate it to all ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    30

    Create file with fixed length and initiate it to all 0's

    Hi all,

    Is there a good way to create a new file with 1024 bytes, and initiate it to all 0's ?

    Thank you...

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    173
    Hmm I'm assuming you want the file of size 1024 bytes with all zeros in it, so you may be looking at:

    Code:
    #include <stdio.h>
    
    #define SIZE 1024
    
    int main(int argc, char **argv)
    {
    	FILE *fp = fopen("sample.txt","w");
    	int i = 0, c = 0;
    
    	if (fp == NULL) {
    		fprintf(stderr,"Could not open for writing!\n");
    		exit(1);
    	}
    
    	for (i = 0; i < SIZE; i++)
    	{
    		c = fputc("0",fp);
    		if (c == EOF)
    			fprintf(stderr,"Error!\n");
    	}
    
    	if(fclose(fp) != 0)
    	{
    		fprintf(stderr,"Could not close properly!\n");
    		exit(1);
    	}
    
    	return 0;
    }
    I must admit looks pretty horrible and I'm pretty sure someone has a neater solution

  3. #3
    Even death may die... Dante Shamest's Avatar
    Join Date
    Apr 2003
    Location
    Malaysia
    Posts
    970
    If you mean 0 as in character '0'...

    Code:
      char bytes[1024];
      for( int i=0; i<1024; ++i )
        bytes[i] = '0';
      fwrite( bytes, 1, 1024, f );
    Or 0 as in binary 0...

    Code:
      char bytes[1024] = {0};
      fwrite( bytes, 1, 1024, f );

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <unistd.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    
    int main(void)
    {
      char *filename = "slappy.dat";
      char fill[1024];
      int fd;
    
      memset(fill, '0', sizeof(fill));
    
      if((fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT,
        S_IRUSR | S_IWUSR)) == -1)
      {
        puts("Error opening file for writing!");
        exit(1);
      }
    
      if(write(fd, fill, sizeof(fill)) != sizeof(fill))
        puts("Error writing to file!");
    
      close(fd);
      return 0;
    }
    itsme@itsme:~/C$ cat slappy.dat
    00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 00000000000000000000000000000000000000000000000000 000000000000000000000000itsme@itsme:~/C$
    EDIT: Don't ask me why it looks like the 0's are in 2 columns...the forum is doing that for some reason. The 0's are actually all adjacent to each other in my output file.
    If you understand what you're doing, you're not learning anything.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by 0rion
    Code:
    		c = fputc("0",fp);
    I must admit looks pretty horrible and I'm pretty sure someone has a neater solution
    Indeed it does. You see, fputc doesn't take a string as an argument.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21