1

I am trying to read from a file that contains multiple lines of numbers and strings and save it to an array. However when I call upon the elements of values stored in that array, and I got incorrect results.

For example if I have a text file that has

1:Smith:John:3;43
2:Saget:Bob:5:55
3:Wayne:Bruce:7:21

I'm trying to store the objects for the first line in the array in s[0], second line in s[1], and third line in s[3].

Here is my struct SalesPerson

struct SalesPerson {
    int salesNum; // 4-digit integer
    char lastName[31]; // salesperson's last name
    char firstName[31]; // salesperson's first name
    int salesRate; // commission level for each salesperson
    double salesAmount; //amount the salesperson committed for the week   
};

Here is my main function

#include <stdio.h>
#include <stdlib.h>
#include "sales.h"
#define SIZE 1000

int main()
{
    struct SalesPerson s[SIZE];

    //opening the file for reading
    FILE * openRead(char salesinfo[]);
    {
        FILE *fp;
        fp = fopen("salesinfo.txt", "r");

        int i;

        if(fp != NULL){

            //read from the file and save it to array s
            for (i=0;i<SIZE;i++){
                while(!feof(fp)){
                    fscanf(fp, "%d:%[^:]:%[^:]:%d:%lf\n" ,&s[i].salesNum,     s[i].lastName, s[i].firstName, &s[i].salesRate, &s[i].salesAmount);
                }
            }
            fclose(fp);
        }
        else
            printf("ERROR! Cannot open the file!\n");
    }
}

If I do

printf("%d\n", s[i].salesRate);

I get all the results for salesRate.

3
5
7

However, when I try a printf

printf("%d\n", s[1].salesRate);

Instead of giving me 5, I got an incorrect result. Can someone tell me where I am going wrong? Is my for loop condition incorrect?

5
  • Can you provide your entire main function? You've given only half a program... Commented Aug 12, 2015 at 5:48
  • sorry, ive included my entire main function now Commented Aug 12, 2015 at 5:54
  • 1
    See while (!feof(file)) is always wrong for a discussion of why your loop is wrong. TL;DR — test the result of fscanf(); do not use feof(). Commented Aug 12, 2015 at 5:55
  • What is struct SalesPerson ? Could it contain char *lastName ? Commented Aug 12, 2015 at 6:03
  • @SergeBallesta struct SalesPerson has 5 pieces of information that the text im reading from already has. Im trying to create an array that stores those 5 pieces of information. I will post my sturcture int he above question. Commented Aug 12, 2015 at 6:10

2 Answers 2

4

You have an extraneous level of indentation; you really don't need the { after the declaration of the unused openRead() function, or the corresponding }.

You've updated the data to match the code, which isn't entirely fair on ilent2 whose answer was originally pertinent but is no longer as relevant.

You have major problems in the reading loops — one of the problems being that there are two nested loops:

for (i=0;i<SIZE;i++){
    while(!feof(fp)){
        fscanf(fp, "%d:%[^:]:%[^:]:%d:%lf\n", &s[i].salesNum, s[i].lastName, s[i].firstName, &s[i].salesRate, &s[i].salesAmount);
    }
}

Your inner loop suffers from while (!feof(file)) is always wrong. Additionally, you read to EOF for i == 0, and the subsequent reads all fail immediately. (This doesn't tie in with your claimed result: it isn't clear to me what you've done here.) You should be testing that the fscanf() read the correct number of values. So, you need a single loop, and it should look more like:

for (i = 0; i < SIZE; i++)
{
    if (fscanf(fp, "%d:%[^:]:%[^:]:%d:%lf\n", &s[i].salesNum, s[i].lastName,
               s[i].firstName, &s[i].salesRate, &s[i].salesAmount) != 5)
        break;
}
Sign up to request clarification or add additional context in comments.

1 Comment

This has fixed my issue. Thank You
1

If your file has entries like 1;Smith;John;3;43 your fscanf(fp, "%d:%[^:]:%[^:]:%d:%lf\n" should contain ; instead of :. Not sure if this is your only problem, but this is the first that jumps out at me.

You may or may not need the \n at the end either since (from memory) scanf will discard whitespace sometimes (I'll find a link).

1 Comment

The sample data has been updated since this (originally correct) answer was given.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.