Adding a string array to another string array?

This is a discussion on Adding a string array to another string array? within the C Programming forums, part of the General Programming Boards category; How would I go about adding a string array to another string array? I am scanning a file that takes ...

  1. #1
    Registered User
    Join Date
    Mar 2012
    Posts
    5

    Adding a string array to another string array?

    How would I go about adding a string array to another string array? I am scanning a file that takes each word and puts it in to a string array wbuf[]. Then I am sending wbuf[] to a function that checks if there is enough room in the lbuf[62] array. If there is enough room it copies wbuf[] into lbuf[62]. I can get it to copy one word into lbuf[62], but I can get it to continue to add the next words without running in to errors. Should i use sscanf(wbuf "%s", lbuf)? Sorry I am relatively new to programming. Thanks in advance.

    Code:
    
    // Tanner Brandt
    // 002617908
    
    
    #include <stdio.h>
    #include <string.h>
    #define MAX_LINE_LEN 62
    
    
    
    
    int output (char wbuf[]);
    int space_check(int line_len);
    int get_text(void);
    
    
    int lbuf_len = 0;
    
    
        FILE *gettyText;
        FILE *csis2;
    
    
    int main(void){
    
    
        fopen_s(&gettyText, "getty.txt", "r");
        fopen_s(&csis2, "csis2.txt", "w");
    
    
        get_text();
        
        getchar();
        return 0;
    }
    
    
    int get_text(void)
    {
        int n = 0;
        
        char wbuf[10];
        
        while (!feof(gettyText))
        {
            fscanf(gettyText, "%s", wbuf);
            output(wbuf, n);
                printf("test");
                n++;
        }
        return 0;
    
    
    }
    
    
    int output(char wbuf[], int n)
    {
        extern int lbuf_len;
        int wordLength;
        char lbuf[62];
        
        wordLength = strlen(wbuf); // determines the size of the word    
        lbuf_len += wordLength; 
    
    
    
        if(space_check(lbuf_len) > wordLength - 1){
            printf("%s", wbuf);
                    sscanf(wbuf, "%s", lbuf[n]); // This is my problem
            strcat(wbuf, lbuf);              // This area is the problem
            n++;
            puts(lbuf);
            //printf("%s", lbuf[n]);    // This is where I get the error
                }
    
    
        if (space_check(lbuf_len) < wordLength -1){
            lbuf_len = 0;
            printf("Cleared space_remaining");
            getchar();
                }
    
    
        return 0;
    
    
    }
        
    int space_check(int lbuf_len){
    
    
        int space_remaining;
    
    
        space_remaining = MAX_LINE_LEN - lbuf_len;
    
    
        printf("%d", space_remaining);
        getchar();
    
    
        return space_remaining;
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the third argument to sscanf wants to be an address (pointer), not the value of the char.
    line 70 change lbuf[n] to &lbuf[n] or better yet investigate using strncat instead

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Constantly adding characters from an array to a string
    By mconflict in forum C Programming
    Replies: 2
    Last Post: 04-02-2012, 11:38 PM
  2. Replies: 3
    Last Post: 06-10-2011, 07:47 PM
  3. Replies: 1
    Last Post: 12-10-2008, 10:29 AM
  4. convert char** (c string array) to std::string[]
    By umen242 in forum C++ Programming
    Replies: 2
    Last Post: 11-11-2008, 04:52 AM
  5. Adding Scores from a string array
    By MB1 in forum C++ Programming
    Replies: 6
    Last Post: 11-11-2005, 06:27 AM

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