Incompatible string assignment types

This is a discussion on Incompatible string assignment types within the C Programming forums, part of the General Programming Boards category; Gudday all The biggest error I get with my C programming is incompatible assignment types of various flavours. In this ...

  1. #1
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49

    Incompatible string assignment types

    Gudday all
    The biggest error I get with my C programming is incompatible assignment types of various flavours. In this case it is strings.

    Code:
     char rptline[30];   //name of RPT file
     const size_t size = 64;
     struct _finddata_t fileData = {0};
    
    // start a search for all files with an extension of "rpt"
     searchHandle = _findfirst("*.rpt", &fileData);
    // if the search started
     if (searchHandle != -1) {
    // print a banner
    
     rptFile = fopen (rpt_file, "w+");  /* open data file "rptFile.dat" for writing */
     do {
    	// print the file name
    	//puts(fileData.name);
    	printf("----- The .rpt file(s) are %s -----\n", fileData.name);
    	rptline = strcat(fileData.name, ".tmp");
    	printf("--- The tmp rpt file name is % ---\n", rptline);
    	//rename(fileData.name, )
    	fprintf(rptFile, "%s", fileData.name);  /* write RPT file name to rptFile.dat file  */
    		}
    The fragment above shows my latest failure. I am trying to add the extension ".tmp " to a list of *.rpt files that I have found
    e.g. I want to get 1.rpt.tmp, 2.rpt.tmp etc:
    The problem occurs in the line
    Code:
    rptline = strcat(fileData.name, ".tmp");
    The errror message is "error: incompatible types in assignment of `char*' to `char[30]' "
    I assume that it does not accept that fileData.name is a string compatible with what strcat would expect. Yet in the line above viz.
    Code:
    printf("----- The .rpt file(s) are %s -----\n", fileData.name);
    the program had no apparent trouble accepting fileData.name as a string.
    Perhaps the initial assignment of fileData is a problem.

    I find trying to sort what when pointers, contents of pointers etc: are to be used very confusing.

    Ultimately all I want to do is rename existing *.rpt files to *.rpt.tmp. Maybe I am making a mountain out of a molehill?

  2. #2
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    the problem is that rptline is a char[] array and you are trying to assign it a char* , which are not the same.

    Assigning a char* to another only copies the pointers and not the string itself, you need to use
    Code:
    strcpy(char*,const char*) ;
    here's one way
    Code:
    strcat(filedata.name,".tmp");
    strcpy(rptline,filedate.name);
    Spidey out!

  3. #3
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Thanks spidey
    The next problem I have is to rename an existing file that I have created with my code with the original filename using the rename function.
    Code:
    char oldrptname[22];  //name of last rpt file looked at
    struct _finddata_t fileData = {0};
    
    strcpy(oldrptname, fileData.name);
    printf("oldrptname= %s\n", oldrptname);
    rename("error.dat", <oldrptname>);
    How would I put the filename stored as variable 'oldrptname' in the 2nd portion of the rename function?
    I have tried combinations of *oldrptname, &oldrptname etc: but within success.
    As noted earlier I can print out oldrptname successfully.
    Unless having oldrptname as a string array is the problem and it would be better/easier to have it as just a char string?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    This is a crazy idea, but it just might work: why not try "oldrptname"? (EDIT: Obviously, without the quotes when you use it in the program.)

  5. #5
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by tabstop View Post
    This is a crazy idea, but it just might work: why not try "oldrptname"? (EDIT: Obviously, without the quotes when you use it in the program.)
    Seems too simple. Only combination I didn't try. I will edit and run.

    Damn. No apparent change e.g. error.dat is still called error.dat. It works though if I give a name with quotes in rename()

    Found one thing - In a C reference it notes that
    Code:
    #include <cstdio>
    int rename( const char *oldfname, const char *newfname );
    I had a .h after the cstdio.
    Still no apparent change e.g. error.dat is still called error.dat.

    Can I use rename( const char *oldfname, const char *newfname ); just as it is or does it need to be in an equation?
    Last edited by Tigers!; 10-28-2009 at 10:26 PM.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    No C reference will tell you that it's <cstdio>. A C++ reference will tell you that it is <cstdio>, because in C++ it is. In C it is <stdio.h> If you try <cstdio.h> it won't even compile (unless you have, advertently or inadvertently, renamed a header to that name).

    rename returns success/failure, so check the return value. For instance, if error.dat is not in the current working directory, there won't be anything there to rename.

  7. #7
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by tabstop View Post
    No C reference will tell you that it's <cstdio>. A C++ reference will tell you that it is <cstdio>, because in C++ it is. In C it is <stdio.h> If you try <cstdio.h> it won't even compile (unless you have, advertently or inadvertently, renamed a header to that name).

    rename returns success/failure, so check the return value. For instance, if error.dat is not in the current working directory, there won't be anything there to rename.
    Here is my sophisicated error checking. Can we find out what the error actually is?
    Code:
    int status;
    
    status = rename("error.dat", oldrptname);
    	if (status != 0)
    	{
    		printf("error found\n");
    		}
    		else {
    
    			printf("No error found\n");
    		}
    I am having a non-zero error return so a fault is found.
    The only thing I can think of of is that error.dat takes a finite time to appear in the directory. The code is probably looking at the directory before error.dat is created so naturally an error is returned.
    2 alternatives
    1. Have the program check for the existence of error.dat 1st
    2. Have the program wait. It is a shame that this code is done at the end of the program. I have nothing else for the programme to do in the meantime.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Your indentation needs some work, there.

    Probably this may help:
    Code:
    int status;
    status = rename("error.dat", oldrptname);
    if (status != 0) {
        perror("Problem renaming:");
    }

  9. #9
    Registered User Tigers!'s Avatar
    Join Date
    Jun 2009
    Location
    Melbourne, Australia
    Posts
    49
    Quote Originally Posted by tabstop View Post
    Your indentation needs some work, there.

    Probably this may help:
    Code:
    int status;
    status = rename("error.dat", oldrptname);
    if (status != 0) {
        perror("Problem renaming:");
    }
    Code:
    status = rename("error.dat", oldrptname);
    if (status != 0)
    {
      perror("problem is:");
    }
     else {
               printf("No error found\n");
            }
    The error message is : File exists.
    I am wondering is it is a permissions problem e.g. the program does not have the authority to cahnge the file's name.
    See attachment
    Attached Images Attached Images  

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    So it won't rename a file to a name that already exists. If oldrptname, whatever its value is, is a file that is already there, then nothing is going to happen. Pick a different name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09:19 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 11:47 AM
  4. Incompatible types in assignment error
    By Zildjian in forum C Programming
    Replies: 12
    Last Post: 10-03-2003, 01:15 PM
  5. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM

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