Thread: f-scanf into different data type arrays

  1. #1
    Registered User
    Join Date
    Nov 2021
    Posts
    1

    Question f-scanf into different data type arrays

    Hi there, i am new to C and programming and while learning File I\o and arrays i am required to scan in 3 different types of data from a file and store them in arrays.

    I believe my code is right but when i try to check if my arrays contain any data by printing them out i get nothing.

    what am i missing here?

    Code:
    #include<stdio.h>
    #include<string.h>
    #defineSIZE120
    
    int main(int argc, char *argv[])
    {
      char line, *nameArray;
      int i, age, ageArray, counter = 0;
      float wage, wageArray;
      FILE *srcFile;
    
      srcFile = fopen(argv[1], "r");
    
      if (srcFile == NULL) {
        printf("Error, no file was opened.\n");
        fclose(srcFile);
        return (0);
      } else {
        printf("File was successfully opened.\n");
      }
    
      while (fscanf(srcFile, "%s%d%f\n", nameArray, &ageArray, &wageArray) != EOF) {
        nameArray = line;
        ageArray = age;
        wageArray = wage;
        i++;
        counter++;
      }
      for (i = 0; i < counter; i++) {
        printf("%s%d%.2f", nameArray, ageArray, wageArray);
      }
      printf("\n%d lines", counter);
      fclose(srcFile);
    
      return (0);
    }
    Last edited by Salem; 11-08-2021 at 11:34 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,119
    Well all the things you've named as arrays are not arrays.
    Plus, actual arrays need subscripts.

    Code:
    #include<stdio.h>
    #include<string.h>
    #define SIZE 120
    #define MAX_LEN 20
    
    int main(int argc, char *argv[])
    {
      char line[MAX_LEN], nameArray[SIZE][MAX_LEN];
      int i, age, ageArray[SIZE], counter = 0;
      float wage, wageArray[SIZE];
      FILE *srcFile;
     
      srcFile = fopen(argv[1], "r");
     
      if (srcFile == NULL) {
        printf("Error, no file was opened.\n");
        fclose(srcFile);
        return (0);
      } else {
        printf("File was successfully opened.\n");
      }
     
      while (fscanf(srcFile, "%s%d%f\n", line, &age, &wage) != EOF) {
        strcpy(nameArray[counter],line);
        ageArray[counter] = age;
        wageArray[counter] = wage;
        counter++;
      }
      for (i = 0; i < counter; i++) {
        printf("%s%d%.2f", nameArray[i], ageArray[i], wageArray[i]);
      }
      printf("\n%d lines", counter);
      fclose(srcFile);
     
      return (0);
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 17
    Last Post: 08-05-2018, 06:59 PM
  2. Replies: 1
    Last Post: 02-10-2012, 05:42 AM
  3. Replies: 2
    Last Post: 05-14-2011, 09:26 PM
  4. store string data as a short or other data type
    By robin2aj in forum C Programming
    Replies: 5
    Last Post: 04-07-2010, 11:02 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread