Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

C problem

Options
  • 08-02-2005 8:17pm
    #1
    Closed Accounts Posts: 3


    Im trying to take the following type of data from a file(*.phn) and storing it in 2 int arrays and an array of strings
    0 2360 h#
    2360 5263 sh
    5263 7021 iy
    7021 8370 hv
    8370 1023 tcl
    1023 1260 h#

    Im trying to acommplish this using the following code
    fpointer = fopen("sa1.phn","r");
    while(!feof(fpointer))
    {
    fscanf(fpointer,"%d %d %s",&start[count],&stop[count],label[count]);
    count = count + 1 ;
    }
    fclose(fpointer);

    When i try to run this code windows tells we that the program has done something illegal and an error log has been created.I think it has something to do with the third row of characters being taken in but i dont know what?

    Does anybody know what is wrong or have any advice!!


Comments

  • Registered Users Posts: 950 ✭✭✭jessy


    what is label? if its an array of char then your trying to read a string into a char.


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    I got it to work using the following code (incl declarations) on Linux with gcc 3.3.3.
    #include <stdio.h>
    
    int main()
    {
        FILE * fpointer;
        int start[10], stop[10];
        char label[10][10];
        int count = 0;
        int items;
    
        fpointer = fopen("sa1.phn","r");
        /* Maybe have a do/while loop instead so feof() check at end? */
        while(!feof(fpointer))
        {
            items = fscanf(fpointer,"%d %d %s",&start[count],&stop[count],label[count]);
            /* fscanf returns EOF when it hits end of file. */
            if ( items != EOF )
            {
                /* Just to see what was parsed. */
                printf( "%d: %d,%d,%s\n", items, start[count], stop[count], label[count] );
                count++;
            }
        }
        fclose(fpointer);
    
        return 0;
    }
    

    The problem might be, as jessy mentioned, the declaration of 'label'.


  • Closed Accounts Posts: 3 fyp


    Thanks very much for your suggestions Jessy and daymobrew my C is rusty ,and i have been progrmming in matlab for a long while i think it was my declaration of label which was changed and now works.

    Unfortunately i have encoutered another problem taking in the data.And i was wondering if anybody knew what was going on
    This data is what i want to bring into the program
    0 3050 h#
    3050 4559 sh
    4559 5723 ix
    5723 6642 hv
    6642 8772 eh
    8772 9190 dcl
    9190 10337 jh
    10337 11517 ih
    11517 12500 dcl
    12500 12640 d
    12640 14714 ah
    14714 15870 kcl
    15870 16334 k
    16334 18088 s

    And it comes out like this
    0 3050 h#
    3050 4559 sh
    4559 5723 ix
    5723 6642 hv
    6642 8772 z9
    8772 9190
    9190 10337 *«
    10337 11517 ih
    7103332 12500 dcl
    12500 12640
    12640 14714 y¥
    14714 15870 kcl
    15870 16334 %
    16334 18088 s

    Also while im at it does anybody know how to take in a wave file into a c programme?


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    What did you declare 'label' to be?
    Also while im at it does anybody know how to take in a wave file into a c programme?
    What exactly do you want to do with it? Play it perhaps?
    You *could* find out the size of the file, allocate memory and read it all it. But that might not be any use. All depends on what you want to do with it.


  • Registered Users Posts: 950 ✭✭✭jessy


    format of wave file
    http://ccrma.stanford.edu/CCRMA/Courses/422/projects/WaveFormat/

    should be very easy once you know the format.


  • Advertisement
  • Closed Accounts Posts: 3 fyp


    Concerning the wave file (which is a line of speech) i wish to take it into a program and segment the speech using the sample numbers in the file as indexe to the wave file.
    Another problem is i want to store different length vectors of sampled speech data and i was thinking of using an array of pointers to int
    Any suggestions

    label was declared as follows:
    char label[3][50];
    fscanf(fpointer,"%d %d %s",&start[count],&stop[count],label[count]);


  • Registered Users Posts: 6,508 ✭✭✭daymobrew


    label was declared as follows:
    char label[3][50];
    No wonder it failed.
    You declared 3 elements of 50 chars. I'm guessing you wanted 50 elements of 3 chars.
    That is:
    char label[50][3];
    
    but you need to store the trailing NULL char so you need 4 spaces to store 'dhl' (4th space for '\0' (aka NULL char).

    So I recommend changing the declaration to
    char label[50][4];
    


Advertisement