Thread: Issue creating a string from file using strcat

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Issue creating a string from file using strcat

    I'm trying to read a column of random sized order numbers from a file and put them in one long string that will be used later. each order needs to be separated by a ",".

    Ex:
    000111222-1112233
    002233-393900

    Output:
    Value=000111222-1112233,002233-393900

    This is what i have so far, but it definitely is messed up... any help is greatly appreciated!

    Code:
    int i;
    long file;
    char longOrderID[300];
    
         /* Create a new file */
         if ((file = fopen(filename, "r" )) == NULL) {
    
              lr_output_message("Unable to create %s", filename);
              return -1;
         }
    
         strcpy(longOrderID, "Value=");
    
    	 for(i=0;i<=2;i++){
    
    	fscanf(file, "%s", longOrderID);
                    strcat(longOrderID, file);
    	strcat(longOrderID, ",");
                    mktemp(longOrderID);
    
    	i++;
    
                    }

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
         /* Create a new file */
         if ((file = fopen(filename, "r" )) == NULL) {
    
              lr_output_message("Unable to create &#37;s", filename);
              return -1;
         }
    Doesn't create a new file, in-fact it will fail if the file doesn't exist.

    Do you really think 'longOrderID' will be big enough to hold the entire file?

    Do you mean turn a file such as:

    1234567
    5451213
    1231231
    1421241

    Into, another file:
    Value=1234567,5451213,1231231,1421241

    Or you plan to not save 'Value' at all?

    You might consider reading the lines as:
    Code:
    char line[256];
    
    /* open file */
    
    while(fgets(line, sizeof(line), file) != NULL)
    {
        /* add line to 'Value' */    
    }
    That way it doesn't matter how many order id's are in the file.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    What you are doing is using the result string to read input into the buffer, which is causing you to overwrite parts of what you want.

    You could do this with minimal extra space if you are confident you have allocated enough memory. Here's one untested idea:
    Code:
    char longOrderID[300] = { '\0', };
    char * p = longOrderID; /** position in longOrderID **/
    char * nl = NULL;       /** find newline characters **/
    
    size_t read, bufsiz;
    
    strcpy( p, "Value=" );
    bufsiz = sizeof longOrderID;
    read = strlen( p );
    p += read;
    bufsiz -= read;
    
    /** read in chunks: **/
    while( bufsiz > 0 && fgets( p, bufsiz, file ) != NULL ) {
       if ( ( nl = strchr( p, '\n' ) ) != NULL )
          *nl = '\0';
    
       strcat( p, "," );
       read = strlen( p );
       p += read;
       bufsiz -= read;
    }
    That should be fairly straight forward, but I've been wrong before.
    Last edited by whiteflags; 02-21-2008 at 12:16 AM.

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    4

    Thanks for the suggestions, but getting memory violation

    Thanks for the help so far!

    Unfortunately, I keep getting a memory violation at the 'fgets' section (when using either of the two suggestions)

    any more assistance would be much appreciated!

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by wormsworm View Post
    Thanks for the help so far!

    Unfortunately, I keep getting a memory violation at the 'fgets' section (when using either of the two suggestions)

    any more assistance would be much appreciated!
    Show your current code
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    Sorry, this is what i have so far: (this is for a Loadrunner script, so some code with LR_ is made for that program)

    This code does work except for finding the '/n' within this code
    if ( ( nl = strchr( p, '\n' ) ) != NULL )
    *nl = '\0';

    I'm getting an error: "operands of = have illegal types `pointer to char' and `int'"

    if i comment out the if statement, the code works, but there is a return after each order id (which i do not want)

    Code:
    Action() {
    
    char *filename = "Inbound_OrderID_Copies.txt";
    long file;
    char longOrderID[30000] = { '\0', };
    char * p = longOrderID; /** position in longOrderID **/
    char * nl = NULL;       /** find newline characters **/
    
    size_t read, bufsiz;
    
    strcpy( p, "Value=" );
    bufsiz = sizeof longOrderID;
    read = strlen( p );
    p += read;
    bufsiz -= read;
    
    /** read in chunks: **/
        if ((file = fopen(filename, "r")) == NULL ) {
    
              lr_error_message("Cannot open &#37;s", filename);
              return -1;
         }
    
    while( bufsiz > 0 && fgets( p, bufsiz, file ) != NULL ) {
       if ( ( nl = strchr( p, '\n' ) ) != NULL )
         *nl = '\0';
    
       strcat( p, "," );
       read = strlen( p );
       p += read;
       bufsiz -= read;
    
    
    	 lr_output_message("OrderID = %s", longOrderID);
    }
    	
         return 0; 
    
    }

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    file should be

    FILE* file;
    about your warning - check the include headers - probably you are missing several - this could be a reason for crash
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  8. #8
    Registered User
    Join Date
    Feb 2008
    Posts
    4
    vart, you are exactly correct! I'm using Loadrunner software which contains it's own header files and I needed to copy a "studio.h" from another program into Loadrunner.

    That fixed the problem and the script is working perfectly!

    Thank you very much "citizen" for the great assistance!

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Oh yes, give all your functions a return type. Always.
    And try indenting a little better. If you need a little assistance, try this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM