1

I'm doing my school assignment and I've got issues with fscanf, file management so to speak. I've been at this for hours, looking up various functions and fscanf itself, and I'm still unable to solve this problem.

#include<stdio.h>

int main()
{
    FILE *f3;
    float amount_with[100][30];
    int j;
    char date_with[100][30];

    f3 = fopen("withdrawals.txt", "r");
    if (f3 == NULL)
    {
        printf("ERROR! File could not be opened.\n");
    }

    for(j=0; j <= 50 || !feof(f3); j++)
    {
       fscanf(f3, "%s %f", date_with[j], &amount_with[j]);
       printf("%d %s RM %.2f", j+1, date_with[j], amount_with[j]);
    }
}

I want to feed into the program a line of data of floats, integers and strings. (eg. 18/11/18 200.00 within this program) from a text file via fscanf, or any other possible way. However when I execute the code, the program hangs and I am unable to do anything within it other than CTRL+C.

Thanks for any help.

  • 4
    Using `!feof(fp)` to control a loop is always wrong. Using `||` instead of `&&` is wrong too. – Jonathan Leffler Nov 28 '18 at 13:11
  • 3
    You have a stray `;` at the end of your `for`. VTC as a typo. – dbush Nov 28 '18 at 13:13
  • @JonathanLeffler Would using while(!feof(fp)) and having j increment within it be best? – partynextdoor18 Nov 28 '18 at 13:14
  • 1
    modern compilers usually warn about this semicolon typo – Jean-François Fabre Nov 28 '18 at 13:14
  • @partynextdoor18 first remove the ";" after the "for". The rest is detail in comparison – Jean-François Fabre Nov 28 '18 at 13:15
  • @Jean- Done. Ugh didn't notice it. Silly mistake. I'm using C-Free 4.0 as my IDE... not sure why it didn't warn. – partynextdoor18 Nov 28 '18 at 13:18
  • because the level of warnings should be at `-Wall -Wextra -Werror` (possibly `-pedantic`) and aren't. When starting C, always set this setting (and afterwards too). The compiler never warns for nothing. Also [edit] your post to remove the ";" in the question so we can focus on the second issue – Jean-François Fabre Nov 28 '18 at 13:19
  • 1
    It would be best not to use `feof()` at all. Test whether the input operation succeeds. Check the result from `fscanf()` directly. Most experienced C programmers hardly ever use `feof()` at all. – Jonathan Leffler Nov 28 '18 at 13:19
  • 1
    As a style suggestion, always put the opening brace of a block statement on the same line as the statement that starts it. Doing so greatly reduces if not eliminates this kind of bug. – dbush Nov 28 '18 at 13:19
  • @Jon Could you elaborate further? I've been taught to use `!feof()` in class, so I'm not sure what else I could have as a control besides `EOF == NULL` and maybe `feof()` (?) – partynextdoor18 Nov 28 '18 at 13:25
  • I’m sorry that your teacher is teaching you appallingly awful habits. Please send your tutor on a remedial C course. There’s a standard question on why not to use it; it’s easy to locate it on a laptop browser but not in the iPhone app. – Jonathan Leffler Nov 28 '18 at 13:28
  • @xing I want to have multiple strings of the same variable, would having just a 1d array work? – partynextdoor18 Nov 28 '18 at 13:28
  • 1
    Try https://stackoverflow.com/q/5431941/15168 – Jonathan Leffler Nov 28 '18 at 13:31
  • @xing Ah my bad. The data types. So for everything else other than a string I would leave it to just the number of rows... – partynextdoor18 Nov 28 '18 at 13:41
  • @Jonathan Thanks, I was already on it. Can you recommend any online resources I could refer to to learn programming logic in general, and not just C? I've been given the site automatetheboringstuff.com by a friend to learn Python, but I'm planning on looking into it just to learn logic. – partynextdoor18 Nov 28 '18 at 13:47
  • @xing Yes sorry a mistake. Will edit it. – partynextdoor18 Nov 28 '18 at 13:48
  • Whe I was learning to program, the web didn’t exist. I’ve not gone looking for such sites. I don’t have any recommendations — other than SO but it doesn’t give a coherent ordered tutorial presentation. – Jonathan Leffler Nov 28 '18 at 13:50
  • Regarding the feof function usage, I am not going to comment if it's OK to avoid it. However, if you want to use it, then make sure that no logic errors are made. here is what I mean. j <= 50 || !feof(f3); What you are doing here is, as long as j <= 50, you will skip the feof function check!! This is a serious problem. Instead, the condition should be j <= 50 && !feof(f3) Or, skip feof altogether and verify fscanf, like already suggested by other comments. – Krassi Em Dec 02 '18 at 08:44
  • change two functions in for to: `fscanf(f3, "%s%f", date_with[j], amount_with[j]); printf("%d %s RM %.2f", j+1, date_with[j], *amount_with[j]);` – EsmaeelE Dec 26 '18 at 17:23
  • Compiling your code with gcc flags like this lead to that change: `gcc -Wall -Wextra -pedantic-errors code.c -o code` – EsmaeelE Dec 26 '18 at 17:25

0 Answers0