Array of Character Arrays

This is a discussion on Array of Character Arrays within the C Programming forums, part of the General Programming Boards category; Ok, I am running into a brick wall so time to ask- I have a test driver. Up to this ...

  1. #1
    Unregistered
    Guest

    Question Array of Character Arrays

    Ok, I am running into a brick wall so time to ask-

    I have a test driver. Up to this point it runs on one data file, but I wanted to make it able to run a series of tests. So I have made a file that contains the full names of all tests I want to run
    (e.x. c:\test1 c:\test2)

    So I fopen this file and want to start reading these character arrays into... an array. Here is where it gets sticky. I tried making a two dimensional array and I can't seem to fill it in. Here is my code snippet:

    if ((Data_File= fopen(Tests_File,"r")) != NULL)
    {
    // while we are still in test input data section, loop
    stop=0;
    test_num= 0;

    while (stop == 0)
    {
    // Get test location out of file
    fscanf(Data_File, "%s", &File_Location);

    if (!strcmp(Command,"end:")) stop= 1;
    else
    {
    strcpy (Test_Name[i][0], Command);
    test_num++;
    } // end else
    } // end while
    } // end if

    fclose(Data_File);

    for (i= 1 ; i <= test_num ; i++)
    {

    Run_Test(Test_Name[i-1]);
    }

  2. #2
    Unregistered
    Guest

    Unhappy

    crap- I hit tab and post... a cleanup of the code snippet to follow...

    // array of tests that will be run
    char Test_Name[20][60];

    if ((Data_File= fopen(Tests_File,"r")) != NULL)
    {
    // while we are still in test input data section, loop
    stop=0;
    test_num= 0;

    while (stop == 0)
    {
    // Get file name of file to run test on
    fscanf(Data_File, "%s", &File_Name);

    // at end of "tests" file I put a end: to know I am done
    // and to allow me to put comments after it
    if (!strcmp(File_Name,"end:")) stop= 1;
    else
    {
    strcpy (Test_File[i], File_Name);
    test_num++;
    } // end else
    } // end while
    } // end if

    fclose(Data_File);

    for (i= 1 ; i <= test_num ; i++)
    {
    Run_Test(Test_File[i-1]);
    }

    and what happes is that strcpy from File_Name to Test_File[i] always copys to the first row of the Test_File array. what I am trying to get it to do is write it so I have an array of strings that I can then "Run_Test" on each test file that I read out of the main test file...

    sheesh, that was long winded- ask if you need more clarification

  3. #3
    varm
    Guest
    Try this:

    Open your file, and count the lines with the help of fgets( use a dummy variable). rewind the filepointer, and using fgets again, this time reading each string into the new array...
    Code:
    while(!foef(flpntr))             {
    fgets(ignoreThisVariable, 100, flpntr);
    countOfLines++;                }
    
    char array[countOfLines][100];
    
    rewind(flpntr);
    
    int i = 0;
    
    while(!feof(flpntr))  {
    fgets(array[i++], 100, flpntr);  }
    
    fclose(flpntr);
    
    //done!

  4. #4
    Budding Synth Programmer samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Trefforest
    Posts
    368
    Take away the [0] and leave the [i]. This will pass the pointer of the string to strcpy, which is what you want. [i] [0] will pass the first char literally.
    MSVC++ 6.0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. erase all characters in a character array?
    By diddy02 in forum C Programming
    Replies: 4
    Last Post: 11-20-2008, 04:30 PM
  2. Replies: 8
    Last Post: 11-12-2008, 10:25 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Selection Sort on 2-D character array
    By TankCDR in forum C++ Programming
    Replies: 6
    Last Post: 07-20-2003, 11:59 AM
  5. confuse with character array
    By dv007 in forum C Programming
    Replies: 6
    Last Post: 08-09-2002, 01:05 PM

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