Thread: threads

  1. #1
    Unregistered
    Guest

    threads

    Would a message saying Thread stopped be due to a file being unable to be opened or code being written wrong. Here's my problem.
    void read(name_t name[], int *num_records)
    {
    int i;
    int flag = 3;
    FILE *input;

    input = fopen("file1", "r");
    check_file(input, "file1");

    while(flag != EOF && flag ==3 && i<MAXIMUM)
    {
    fscanf(input, "%s %s", &name[i].last, &name[i].first);
    fscanf(input, "%s", &name[i].given_name);
    i++;
    }
    *num_records = i - 1;
    fclose(input);
    }

    I am getting a thread stopped at the first fscanf.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Since you never initialized i, num_records is incorrect when you return from function read().

    //int i;

    Change to:
    int i = 0;

    //input = fopen("file1", "r");

    Add some lines to see if fopen() succeeded:
    input = fopen("file1", "r");
    if (input == NULL)
    {
    printf("Unable to open file.\n");
    exit(1);
    }

    //while(input != EOF && flag ==3 && i<MAXIMUM)

    Change to:
    while(!feof(input) && flag ==3 && i<MAXIMUM)
    Last edited by swoopy; 12-06-2001 at 12:14 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 10-17-2008, 11:28 AM
  2. Yet another n00b in pthreads ...
    By dimis in forum C++ Programming
    Replies: 14
    Last Post: 04-07-2008, 12:43 AM
  3. Classes and Threads
    By Halloko in forum Windows Programming
    Replies: 9
    Last Post: 10-23-2005, 05:27 AM
  4. problem with win32 threads
    By pdmarshall in forum C++ Programming
    Replies: 6
    Last Post: 07-29-2004, 02:39 PM
  5. Block and wake up certain threads
    By Spark in forum C Programming
    Replies: 9
    Last Post: 06-01-2002, 03:39 AM