fscanf to fill 2D array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wilco
    New Member
    • Aug 2008
    • 4

    fscanf to fill 2D array

    Hi,
    im filling a 2D array using fscanf and reading from a csv file.
    the problem is the file im reading from has some blank spaces which causes any information following to be output as an error.

    Is there any way of ignoring the blank spaces in the csv file (excel) and continuing to the next number?


    # include<stdio.h >
    # include <stdlib.h>
    # include<math.h>

    int ReadFromFile(in t Array[][30], FILE *inFile);

    int main(void)
    {
    double arrayVal;
    int Array[30][30], i, j, x;
    char filename[50];


    for(i=0;i<28;i+ +)
    {
    for(j=0;j<28;j+ +)
    {
    Array[i][j]=0;
    }
    }
    //Get input filename
    printf("Enter the filename to read:");
    scanf("%50s",fi lename);

    //Open file
    FILE *inFile;
    inFile = fopen(filename, "r");

    if(inFile==NULL )
    {
    printf("Could not properly load file!\n");
    getchar();
    getchar();
    }
    else
    {
    printf("File opened and loaded properly\n\n");
    }

    for(i=0;i<28;i+ +)
    {
    for(j=0;j<28;j+ +)
    {

    Array[i][j]= ReadFromFile(Ar ray, inFile);
    printf("The Array Value at Point %d,%d=%i\n", i+1, j+1, Array[i][j]);
    }
    fscanf(inFile," \n");
    }

    fclose(inFile);

    getchar();
    getchar();

    }


    int ReadFromFile(in t Array[][30], FILE *inFile)
    {
    int value;
    fscanf(inFile," %i,", &value);

    printf("the value=%i\n", value);


    return (value);

    }


    thanks
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    It's been a while but I thought fscanf skipped whitespace so it should skip spaces.

    Question: If you array is double why are you reading int from the file? If the file actually contains a double, fscanf() will croak on the decimal point.

    Question: Why are you reading into tour array with two indexes? There really are no multi-dimensional arrays in C or C++. You might read: http://bytes.com/forum/thread772412.html.

    Comment

    • wilco
      New Member
      • Aug 2008
      • 4

      #3
      no its not skipping the white spaces cause when i fill the spaces with numbers the program works.
      -cant fill the spaces with numbers all the time as that would cause way too much work.

      I need a multidimensiona l array to keep information in same order as it is in the csv file

      Comment

      Working...