0

I need to scan 3 strings and an integer from an already existing file, and save them each as an element in an array of structures. The file is formatted as such:

string1 string2 integer string3

string1 string2 integer string3

... and so on.

when fscanf-ing for them, it correctly scans string1, the integer, and string3. However, string2 always appears to create an error when scanning it, and when any of the string2's are printed, they are either cut off short, or have some odd ASCII symbol, like a question mark in a box; it also sometimes triggers the system "bell" sound.

I've tested collecting the strings through fscanf for each different one, and it is only the second one that ever messes up.

A stripped-down version of my code, highlighting the issue

struct carinfo
{
    char name[10];
    char make[15];
    int number;
    char color[10];
 }car[4];

filepointer = fopen("file.txt", "r");

while(!feof(filepointer))
{
   for(i=0;i<4;i++)
   { 
       fscanf(filepointer, "%s %s %d %s", &car[i].name[10], 
      &car[i].make[15], &car[i].number, &car[i].color[10]);

       printf("%s\n", &car[i].make[15]);
   }
}

my expected result is that the second string will scan properly, and be stored in its entirety to its element in the array of structures; instead, the value is usually cut off (at around 2-3 characters instead of 15) and contain weird ASCII symbols.

  • 4
    `&struct[i].string1[10]` does not read ten characters into a buffer, it reads characters into the buffer **starting at the 11th character position**. If this is your intent, there's probably a better way of expressing it, as this code looks really strange. Can you show the declaration for your "struct". `struct` is a reserved keyword, so that can't compile in its current form. – tadman Apr 11 '19 at 01:31
  • The beeps and blorps you're hearing are probably uninitialized character buffer data which tends to be just random garbage, but unless you show a bit more code for context it's hard to say. – tadman Apr 11 '19 at 01:33
  • 1
    @tadman code has been updated to better represent my actual code – TheyThinkThatImTomCruise Apr 11 '19 at 01:48

1 Answers1

0

Changing fscanf() function to this, will solve your issue.

Code

fscanf(filepointer, "%s %s %d %s", car[i].name, 
       car[i].make, &car[i].number, car[i].color);

You can find true way of using fscanf() in cplusplus.com:

char str [80];
FILE * pFile;
pFile = fopen ("myfile.txt","w+");
fscanf (pFile, "%s", str);

But as chris-dodd stated at comment section using feof(filepointer) inside while loop not correct.

This code below i correct it by using fscanf() return value.

I obtain this way in a stack overflow answer by lio

Complete version of your program

main.c

#include <stdio.h>

struct carinfo
{   
    char name[10];
    char make[15];
    int number;
    char color[10];
}car[4];


int main(){
    
int i=0;    
    
FILE* filepointer = fopen("file.txt", "r");

if(!filepointer){
    printf("can't open file\n");
}

int ret=0;

do {
    for(i=0;i<4;i++){      
        ret=fscanf(filepointer, "%s %s %d %s", &car[i].name, 
        &car[i].make, &car[i].number, &car[i].color);
        printf("%s\n", &car[i].make);
    }
} while (ret != EOF);

    return 0;
}

file.txt

salam beshoma 10 daram
kee innak 15 inomikhonid!
Community
  • 1
  • 1
EsmaeelE
  • 2,331
  • 6
  • 22
  • 31
  • This didnt seem to work; after this, it saved the string as just 10 empty characters, and didnt actually save the characters from the file. I didnt have time to test it with the other strings as I'm heading to bed. – TheyThinkThatImTomCruise Apr 11 '19 at 02:15
  • @TheyThinkThatImTomCruise No, Its put first string into the name char array and insert a `\0` at the end of it. – EsmaeelE Apr 11 '19 at 02:25
  • 2
    See [why is while(!feof always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Chris Dodd Apr 11 '19 at 02:40
  • @ChrisDodd Ok, that's good capture, thank you, i read this before, I will correct that mistake. – EsmaeelE Apr 11 '19 at 02:46