reading in file data

This is a discussion on reading in file data within the C Programming forums, part of the General Programming Boards category; I'm new to c, and i've been given a project that requires me to read in data from files of ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    3

    reading in file data

    I'm new to c, and i've been given a project that requires me to read in data from files of varying length. i then put the data into an array and use it in some calculations. basically im having trouble with how to make the program realise these .txt files have different amounts of data in them and scan in only the appropriate amounts of information. is there a general way to read in file data without having to define how many pieces of data there are first? heres a section of my program where iv told the program the maximum number of bits of data, but this isn't working.
    thanks

    Code:
    void filename()
    {              /*entering the data from an existing file*/
    char file[81];
    puts("\nEnter the name of the file u wish to use\n");
    scanf("%s", file);
    path=fopen(file,"r");
    
    fscanf(path, "%f%d%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f%f", &width, &steps, &y0, &y1, &y2, &y3, &y4, &y5, &y6, &y7, &y8, &y9, &y10, &y11, &y12, &y13, &y14, &y15, &y16, &y17, &y18, &y19, &y20, &y21, &y22, &y23, &y24, &y25, &y26, &y27, &y28, &y29, &y30);
    
    printf("\nWidth of Beam = %fmm\n",width); 
    printf("\nNumber of steps = %d\n\n",steps);
    
    calculations();
    }
    void calculations()
    {  
    float array[33];
     
    /*Putting data into an array*/
    array[0] = width;
    array[1] = steps;
    array[2] = y0;
    array[3] = y1;
    array[4] = y2;
    array[5] = y3;
    array[6] = y4;    
    array[7] = y5;
    array[8] = y6;
    array[9] = y7;
    array[10] = y8;
    array[11] = y9;
    array[12] = y10;
    array[13] = y11;
    array[14] = y12;
    array[15] = y13;
    array[16] = y14;
    array[17] = y15;    
    array[18] = y16;
    array[19] = y17;
    array[20] = y18;
    array[21] = y19;
    array[22] = y20;
    array[23] = y21;
    array[24] = y22;
    array[25] = y23;
    array[26] = y24;
    array[27] = y25;    
    array[28] = y26;
    array[29] = y27;
    array[30] = y28;
    array[31] = y29;
    array[32] = y30;

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,062
    You should be using a loop. Any why store into an array at the end? Why not just read right into the array from your fscanf()?
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    3
    what sort of loop should i be looking at?can you give me an example, im really not understanding this bit of programming. i stored into an array because i need to then use some arithmetic to alter the array values then loop the new values into some further calculations
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. reading data from a file
    By brianptodd in forum C++ Programming
    Replies: 1
    Last Post: 11-07-2003, 05:50 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

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