1

After reading through other examples here on stack overflow, i'm confused as to where i'm going wrong with my code. I'm trying to read the file and store the 3 pieces of information from each line as data in a struct

e.g. (startTown endTown energyExpended) would be (York Hull 60) for the first line

and then this would be stored in the array allTowns[1024]

any help would be really appreciated, thank you :)

This is the text file i'm trying to read into the struct

This is a link to what the code looks like (is only a few lines)

here is a post in plain text of my code:

  struct intertownEnergy {
    char startTown[256];
    char endTown[256];
    int energyExpended;
  }

  allTowns[1024];

  int openEnergyFile(void) {
    FILE * energyFilePtr = fopen("energy.txt", "r");
    int i = 0;
    if (energyFilePtr == NULL) {
      printf("no such file.");
      return 0;
    }

    while (!feof(energyFilePtr)) {

      fscanf(energyFilePtr, "%s,%s,%d\n", & allTowns[].startTown, & allTowns[].endTown, & allTowns[].energyExpended);
      printf("energy test for each line: %d\n", allTowns.energyExpended);
    }

    return 0;
  }

here is the text file:

York Hull 60 Leeds Doncaster -47 Liverpool Nottingham 161 Manchester Sheffield 61 Reading Oxford -43 Oxford Birmingham 103 Birmingham Leicester 63 Liverpool Blackpool 79 Carlisle Newcastle 92

SOLUTION People in the comments helped me to identify the solution to my problem, so I have edited the code and am posting the correct running code below:

struct intertownEnergy{
char startTown[256];
char endTown[256];
int energyExpended;

} allTowns[1024];


int openEnergyFile(void)
{
FILE* energyFilePtr = fopen("energy.txt","r");
int i = 0;
if (energyFilePtr==NULL){
    printf("the file doesn't exist");
    return 0;
}

while(!feof(energyFilePtr)){

    fscanf(energyFilePtr,"%s%s%d\n", &allTowns[i].startTown, &allTowns[i].endTown, &allTowns[i].energyExpended);
    printf("energy test for each line: %d\n", allTowns[i].energyExpended);
    i++;
}

return 0;

}

Thne
  • 33
  • 4
  • 3
    Don't link to pictures, post your code and your source here in plain text. You are welcome. – paladin Mar 09 '21 at 18:11
  • 1
    What's wrong with the code you already have? – Robert Harvey Mar 09 '21 at 18:23
  • I've taken the liberty of running your code through [this formatter](https://codebeautify.org/c-formatter-beautifier). – Robert Harvey Mar 09 '21 at 18:25
  • @Robert Harvey It says it expects an expression in the square brackets on line 23, (the fscanf line) – Thne Mar 09 '21 at 18:26
  • Ah, quite right. It needs an index into that array, and you didn't provide one. – Robert Harvey Mar 09 '21 at 18:26
  • I wouldn't expect that to do much. Each read from the file will overwrite the previous one. Learn about arrays and how to use them: how to properly declare an array, how to traverse it with an index. – Robert Harvey Mar 09 '21 at 18:30
  • Also thank you for formatting for me – Thne Mar 09 '21 at 18:30
  • 1
    I see three mistakes. [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) Control the loop with `while(fscanf(...) == 3)`. Secondly, remove the commas and the `\n` from the format string (the text file has no commas).Third, you have not supplied an array index. – Weather Vane Mar 09 '21 at 18:32
  • You need an index and to keep count of the Towns read so far. And wait until someone edit the input file and add Stanstead Abbotts, Great Amwell, Much Haddam, Saint Albans.. – arfneto Mar 09 '21 at 18:33
  • 1
    Yes, `%s` stops at a the first whitespace. If there is a town with two words you are in trouble and need to format the file with tabs or commas. – Weather Vane Mar 09 '21 at 18:36
  • @WeatherVane thank you I have solved the problem, is there a way for me to post the solution should anyone else come across this thread and need help? – Thne Mar 09 '21 at 18:48
  • You can edit the question and add it, or self-answer. – Weather Vane Mar 09 '21 at 18:52
  • `while(!feof(energyFilePtr))` cannot be good. You must check the value returned from `fscanf` instead. – M. Nejat Aydin Mar 10 '21 at 05:08
  • the posted code does not compile! Amongst other things, it is missing the needed `#include` statement(s) for the needed header file(s) – user3629249 Mar 10 '21 at 07:16
  • [why while(!feof(energyFilePtr)){ is always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user3629249 Mar 10 '21 at 07:19

0 Answers0