Assigning a value to a char

This is a discussion on Assigning a value to a char within the C Programming forums, part of the General Programming Boards category; Hi, In a while loop I need to clear out a value in a char[20] data type and then reassign ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    Assigning a value to a char

    Hi,

    In a while loop I need to clear out a value in a char[20] data type and then reassign another value to it, so that comparisons can be made.

    the char is called folder 1 in the following code, but the compilier errors with "left operand must be l-value".
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <io.h>
    #include <time.h>
    #include <conio.h>
    #include <sys/types.h> 
    #include <sys/stat.h> 
    #include <string.h>
    
     int main(void)
    {
    FILE *fp;
    char filename[20];
    char *test;
    	
    	 
    	 char  folder1[20] = "";
    	 char  buf1[80];
    	 char  buf2[80];
    	 time_t filecreate;
    	 struct tm *ptr;
    	 struct _finddata_t c_file;
    	 long hfile;
    
    
    	
    
    	 if ((hfile = _findfirst("C:\\Program Files\\Microsoft Visual C++ Toolkit 2003\\samples\\*.out",&c_file)) == 1L)
    		 printf("a");
    
    	 else
    	 {
    		 printf("%-12s %.24s % 9ld\n",c_file.name,ctime(&(c_file.time_write)),c_file.size);
    		/* filecreate = ctime(&(c_file.time_write));*/
    		 ptr = localtime(&(c_file.time_write));
    		 strftime(buf1,80, "%d%m%Y",ptr);
    	    	
    		printf("%s",buf1);
    		if(mkdir(buf1) != 0);
    			{}
    		strcat(folder1,buf1);
    		printf("\nfolder1%s",folder1);
    		if(file_copy(c_file.name,buf1)  != 0)
    		{printf("smeg");}
    	 }
    	
    	
    	 
    	 /*_findclose(hfile);*/
    
    /*time to loop the other files with same extension as the first file*/
                while( _findnext( hfile, &c_file ) == 0 )
                {
                    ptr = localtime(&(c_file.time_write));
    		strftime(buf1,80, "%d%m%Y",ptr);
    		printf("\nnewfilename %s", c_file.name);
    		printf("\nnewtime%s",buf1);
    		if (folder1 == buf1 )
    			{
    				if(file_copy(c_file.name,buf1)  != 0)
    					{printf("smeg");}
                       	}
    		else 
    			{
    				if(mkdir(buf1) != 0);
    					{}
    				folder1 = "";				strcat(folder1,buf1);
    				printf("\nfoldera%s",folder1);
    				if(file_copy(c_file.name,buf1)  != 0)
    				 {printf("smeg");}
            		}    
    	}
    
           _findclose( hfile );
    Last edited by super_stripey; 10-06-2004 at 08:09 AM.

  2. #2
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    you have a problem with your program... we have a problem reading it..

    use code tags.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    ok I've made it simple

    Code:
     
    
    #include <stdlib.h>
    #include <stdio.h>
    
    
     int main(void)
    {
    
    	char folder1[20] = "";
    
    	folder1 = "yes";
    
    	printf("%s",folder1);
    
    }
    .

  4. #4
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    Sorry i didn't exactly get what you want , are you trying to clear out a string and replace it with another one? if so then try this :
    Code:
    	
        char folder1[20] = "original string";
        memset(folder1, 0, 20);
        strcpy(folder1, "new string");
    memset() will set all elements of 'folder1' to zero byte so it clears whatever is there. And 'strcpy' just copies the new string to 'folder1'.
    Last edited by Brain Cell; 10-06-2004 at 08:39 AM.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    5

    Thanks

    ..this also helped understans why my strcat() wasn't working in - you have to allocate memory to hold your strings!

  6. #6
    former member Brain Cell's Avatar
    Join Date
    Feb 2004
    Posts
    472
    'memset()' doesn't allocate memory if thats what you mean. In the previous exmaple , it sets the first 20 elements of the string folder1 to 0 bytes therefore clearing it so it can hold another string.
    My Tutorials :
    - Bad programming practices in : C
    - C\C++ Tips
    (constrcutive criticism is very welcome)


    - Brain Cell

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 08:36 AM
  2. Conversion Char To Char * Problem
    By ltanusaputra in forum Windows Programming
    Replies: 3
    Last Post: 03-01-2008, 02:06 PM
  3. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  4. Replies: 7
    Last Post: 06-16-2006, 10:23 PM
  5. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM

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