Thread: Truncate an array?

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    23

    Truncate an array?

    Unbuntu 10.04 Geany IDE

    I have a sort of a unique problem.
    I want to write to a text file using user input with a char array.
    But if the user doesn't write the exact number of characters for the array the text file
    becomes unreadable.
    Say the array is 10 characters long but the user only inputs 6, the compiler adds extra junk
    characters to fill the array making it unreadable by my Linux system, kicking an encoding error
    when I try to open the file.

    So my question is, is it possible to truncate an array?
    Declare an array of sufficient size, then get the actual number of characters form user_input and truncate the array to that number.
    Or at least how do I get rid of the junk characters my compiler puts in to fill the array?

    I tried adding a second array and using strlen on the first one to get the actual number of characters the user inputs, but getting that info into the second array is alluding me.

    My code so far:
    Code:
    #include <stdio.h>
    #include <string.h>
    
    
    
    
    int main(int argc, char** argv)
    {
    	
    	char usr_input[10];
    	int x;
    
    	FILE *fp;
    fp=fopen("//home//rocko//test.txt", "a+");
    printf("Enter text to append to file: ");
    scanf( "%s", &usr_input);
    x = strlen (usr_input);
    
    	if ( x < 10  )
    	{
    		char new_str[x];
    		
    		fwrite(new_str, sizeof(new_str[0]), sizeof(new_str)/sizeof(new_str[0]), fp);
    		
    		return 0;
    		
    	}
    	
    	else
    	{
    		fwrite(usr_input, sizeof(usr_input[0]), sizeof(usr_input)/sizeof(usr_input[0]), fp);
    		
    
    
    }
    
    	
    	return 0;

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Why not just write the correct number of characters... you already know that number 'x'.
    fwrite(usr_input, 1, x, fp);

    Also, your file name should have single forward-slashes.
    Also, you must check if you were able to successfully open the file.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    You store the length of the string in x, so why not just pass x as the 3rd parameter to fwrite()? Then it will always work properly.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    23
    Thanks, that's so much easier.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    fwrite(new_str,sizeof(char),strlen(new_str),fp);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM