Thread: Problems with sprintf

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    43

    Problems with sprintf

    I have 10 input files named in0.txt, in1.txt, .... , in9.txt

    I want to read the content from the files and store them into an array called inputfile[9]. The contents of the input file are as follows: aaaaaa, bbbbbb, ccccccc, etc... Just very simple file content so I can make sure everything is getting stored correctly. (Note, the length of these files varies slightly sometimes, because I entered the text quickly, but that doesn't matter)

    inputFile is actually declared globally as: char * inputFile[19];
    I doubt this is an issue, but figured I'd mention it.

    Code:
     
            FILE * myfile;
            char line[1024];
            int i = 0;
            char str[200];
    
            for(i = 0; i <= 9; i++)
            {
                    sprintf(str, "in%d.txt", i);
                    myfile = fopen(str,"r+");
                    printf("from string: %s\n", str);
    
                    while (!feof(myfile))
                    {
                            fgets(line, 256, myfile);
                    }
    
                    inputFile[i] = line;
                    printf("%s",inputFile[i]);
            }
    
            for(i = 0; i <= 9; i++)
            {
                    printf("%d: %s\n", i, inputFile[i]);
            }
    And this is my output I get...
    from string: in0.txt
    aaaaaaa
    from string: in1.txt
    bbbbbb
    from string: in2.txt
    ccccccc
    from string: in3.txt
    ddddddd
    from string: in4.txt
    eeeeeee
    from string: in5.txt
    fffffff
    from string: in6.txt
    ggggggg
    from string: in7.txt
    hhhhhhh
    from string: in8.txt
    iiiiiii
    from string: in9.txt
    jjjjjjj
    0: jjjjjjj

    1: jjjjjjj

    2: jjjjjjj

    3: jjjjjjj

    4: jjjjjjj

    5: jjjjjjj

    6: jjjjjjj

    7: jjjjjjj

    8: jjjjjjj

    9: jjjjjjj


    The first for loop does it's job! The correct file names are used, and we store the correct file contents into our inputFile array. However, when I try to print out the inputFile array right afterwards, all 10 elements of the array contain the same content?? This makes no sense at all, and I've been debugging for hours now. This was supposed to be a trivial part of my code, but in the programming world, there is no such thing as trivial.

    If someone could please help out or give me some suggestions, I'd greatly appreciate it.
    Last edited by hansel13; 05-18-2010 at 03:48 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    27
    inputFile[i] = line // This line is problem. Use proper copying of string.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    43
    Quote Originally Posted by AdnanShaheen View Post
    inputFile[i] = line // This line is problem. Use proper copying of string.
    What's wrong there? That compiled OK.

    I added the declaration of the inputfile array to make it more clear.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    27
    It compiled ok, but it is giving your wrong result. Try using proper string copy function.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    43
    Quote Originally Posted by AdnanShaheen View Post
    It compiled ok, but it is giving your wrong result. Try using proper string copy function.

    You mean strcpy?

    I replaced
    inputFile[i] = line;
    with
    strcpy(inputFile[i], line);

    But then I got a segmentation fault.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    27
    Have you initialized your "char * inputFile[19];"?
    Seems no, that is why you're getting segfault now.

    Initialize inputFile.
    Read data.
    Copy read data to inputFile
    Print it.

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    43
    Quote Originally Posted by AdnanShaheen View Post
    Have you initialized your "char * inputFile[19];"?
    Seems no, that is why you're getting segfault now.

    Initialize inputFile.
    Read data.
    Copy read data to inputFile
    Print it.
    Did it, set all inputfile elements to equal 0, then ran the code above (with the strcpy used).

    I still get a seg fault, hmm.

    I'm not so sure if that line is the problem. Because it printed out fine the first time inside the first for loop.. I thought it was the sprintf function, but now I honestly have no clue.

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    27
    Can you post all of your code here? I will examine it

  9. #9
    Registered User
    Join Date
    May 2009
    Posts
    43
    Quote Originally Posted by AdnanShaheen View Post
    Can you post all of your code here? I will examine it
    OK, but a lot of my code is not finished yet, so ignore the queue and consumer/producer stuff. The problem I have right now is inside the main. But I'll post it all anyway...

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <ctype.h>
    #include<unistd.h>
    #include<pthread.h>
    
    typedef struct {
            long head, tail;
            int full, empty;
            pthread_mutex_t *mutex;
            pthread_cond_t *notFull, *notEmpty;
    } queue;
    
    char * buffer[4];
    char * inputFile[19];
    void *producer(void* args);
    void *consumer(void* args);
    
    int main()
    {
    
            queue *test;
            pthread_t producerVar;
            pthread_t consumerVar;
            FILE * myfile;
            char line[1024];
            int i = 0;
            char str[200];
            for(i = 0; i <= 19; i++)
            {
                    inputFile[i] = 0;
            }
            for(i = 0; i <= 9; i++)
            {
                    sprintf(str, "in%d.txt", i);
                    myfile = fopen(str,"r+");
                    printf("from string: %s\n", str);
    
                    while (!feof(myfile))
                    {
                            fgets(line, 256, myfile);
                    }
                    //strcpy(inputFile[i], line);
                    inputFile[i] = line;
                    printf("%s",inputFile[i]);
            }
    
            for(i = 0; i <= 9; i++)
            {
                    printf("%d: %s\n", i, inputFile[i]);
            }
            test->full = 0;
            test->empty = 0;
            test->head = 0;
            test->tail = 0;
            test->mutex = (pthread_mutex_t *) malloc (sizeof (pthread_mutex_t));
            pthread_mutex_init(test->mutex, NULL);
    
            pthread_create(&producerVar, NULL, producer, test);
            pthread_create(&consumerVar, NULL, consumer, test);
    
            pthread_join(producerVar, NULL);
            pthread_join(consumerVar, NULL);
    
            return 0;
    }
    
    void *producer(void *arg)
    {
            queue * test = (queue *) arg;
            int i = 0;
    
            printf("IN PRODUCER\n");
    
            pthread_mutex_lock(test->mutex);
            //test->buf[0] = buffer;
            while(inputFile[0][i] != '\n')
            {
                    inputFile[0][i] = toupper(inputFile[0][i]);
                    i++;
            }
            printf("%s\n", inputFile[0]);
            buffer[0] = inputFile[0];
            pthread_mutex_unlock(test->mutex);
    
            return(NULL);
    }
    
    void *consumer(void *arg)
    {
            int i = 0;
            queue * test = (queue *) arg;
    
            printf("IN CONSUMER\n");
    
    
            pthread_mutex_lock(test->mutex);
            printf("%s\n", buffer[0]);
            pthread_mutex_unlock(test->mutex);
    
            return(NULL);
    }

  10. #10
    Registered User
    Join Date
    May 2009
    Posts
    43
    also, I don't know why my loops are set to 9, haha. They were set to 19, but I just wanted to test the code on a smaller sample.

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    27
    for(i = 0; i <= 19; i++)
    {
    inputFile[i] = 0;
    }
    Do you initialize your char* inputFile[19] like this?
    If yes, can you please think on it, where it is pointing?

    Ok let me tell you, first you have to create memory for char* inputFile[19]
    setting inputFile[nIndex] = 0; does not provide memory to it.

    Hint is, new/malloc

  12. #12
    Registered User
    Join Date
    May 2009
    Posts
    43
    Quote Originally Posted by AdnanShaheen View Post
    Do you initialize your char* inputFile[19] like this?
    If yes, can you please think on it, where it is pointing?

    Ok let me tell you, first you have to create memory for char* inputFile[19]
    setting inputFile[nIndex] = 0; does not provide memory to it.

    Hint is, new/malloc
    Why do I have to dynamically create memory? I can't declare the way I did? I see where my initialization doesn't work, but I still don't get why I have to allocate memory..

  13. #13
    Registered User
    Join Date
    May 2010
    Posts
    27
    If you do not allocate memory, your inputFile[nIndex] = line will come in action. Obviously which is once allocated, and inputFile[nIndex] will point to line. Last time your "line" have let say "jjjjj". Hence resulting the correct value.

    In order to get the proper values, you have to allocated the inputFile, and save your data read from each file.

  14. #14
    Registered User
    Join Date
    May 2009
    Posts
    43
    Quote Originally Posted by AdnanShaheen View Post
    If you do not allocate memory, your inputFile[nIndex] = line will come in action. Obviously which is once allocated, and inputFile[nIndex] will point to line. Last time your "line" have let say "jjjjj". Hence resulting the correct value.

    In order to get the proper values, you have to allocated the inputFile, and save your data read from each file.
    Thanks, can I still declare inputFile globally?

  15. #15
    Registered User
    Join Date
    May 2010
    Posts
    27
    You can, if you want. Obviously you're using it in other methods.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. MinGW causing problems with sprintf()
    By sedavidw in forum C Programming
    Replies: 6
    Last Post: 01-14-2010, 05:37 AM
  2. sprintf Wrapping, a tough one
    By AdmiralKirk in forum C++ Programming
    Replies: 3
    Last Post: 02-03-2006, 10:43 AM
  3. sprintf() giving crash to program!!
    By maven in forum C Programming
    Replies: 4
    Last Post: 01-01-2006, 12:26 PM
  4. sprintf in C and C++
    By usu_vlsi in forum C++ Programming
    Replies: 2
    Last Post: 03-14-2005, 04:14 AM
  5. sprintf and sscanf
    By tommy69 in forum C Programming
    Replies: 10
    Last Post: 04-22-2004, 08:00 PM