logo
Tags down

shadow

C fprintf and fscanf saving game state


By : Tim Caldwell
Date : September 16 2020, 10:00 PM
I hope this helps you . You're not skipping over the newlines in your fscanf() format strings. So the second fscanf() will fail because it expects to find the word car immediately after the last y=%d, but there's a newline between them.
Put a space at the beginning of each format string. That will make it skip over any leading whitespace.
code :
bool loadgame(saved* save) {
    FILE* fptr;
    fptr = fopen("savegame.txt", "r");
    if (fptr == NULL)
        return 0;
    fscanf(fptr, "beeseed= %d", &save->beeseed);
    for (int i = 0; i < 5; i++)
    {

        fscanf(fptr, " car %*d pos x= %d y=%d", &save->cars[i].pos.x, &save->cars[i].pos.y);
        fscanf(fptr, " car %*d speed= %d", &save->cars[i].rand);
        fscanf(fptr, " obstacle %*d pos x= %d y=%d", &save->obstacles[i].pos.x, &save->obstacles[i].pos.y);
        fscanf(fptr, " obstacle %*d speed= %d", &save->obstacles[i].rand);
        fscanf(fptr, " frogend %*d= %d", &save->frogend[i]);
    }
    fscanf(fptr, " health= %d", &save->health);
    fscanf(fptr, " worldTime= %lf", &save->worldTime);
    fscanf(fptr, " frog pos x= %d y=%d", &save->frog.x, &save->frog.y);
    fscanf(fptr, " lost frog pos x= %d y= %d rand= %d", &save->lostfrog.pos.x, &save->lostfrog.pos.y, &save->lostfrog.rand);
    fscanf(fptr, " score bee= %d score end= %d", &save->score.bee, &save->score.end);
    fscanf(fptr, " score flag= %d score frog= %d", &save->score.flag, &save->score.frog);
    fscanf(fptr, " score pos= %d score total= %d", &save->score.pos, &save->score.total);
    fclose(fptr);
    return 1;
}


Share : facebook icon twitter icon

How to fscanf from a .csv which is a output of fprintf


By : ouzha90
Date : March 29 2020, 07:55 AM
Any of those help I suspect it's because "%s" specifier in *scanf() family stops scanning when it finds a white space, you can tell fscanf() which specific character to ignore, and it will stop at that character.
I believe the following format string will work
code :
"%99[^,],%d,%99[^,],%99[^,\n]\n"

Using fscanf and fprintf together in C


By : Ignacio Valdés
Date : March 29 2020, 07:55 AM
will be helpful for those in need After fprintf(), the file pointer points to the end of the file. You can use fseek() to set the filepointer at the start of the file:
code :
fprintf(fp,"Hello2 World\n");
fflush(fp);   
fseek(fp, 0, SEEK_SET);
fscanf(fp,"%s %s",x,y);
rewind(fp);
fscanf(fp,"%9s %9s",x,y);

Are fscanf and fprintf buffered in C?


By : stuartp44
Date : March 29 2020, 07:55 AM
To fix this issue When you open a file with fopen, it will be fully buffered.
You can change the buffering before doing any other operations on the file, using setvbuf (reference).

Error using fprintf and fscanf


By : Abdelaziz Oudouch
Date : March 29 2020, 07:55 AM
may help you .
fscanf(input, "%s,%s,%s,%s,%s,%s,%s,%s,%s", d[0].line1, d[0].team1,...
code :
fscanf(input, "%3[^ ,\n\t],%9[^ ,\n\t],...
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void store(FILE *input, FILE *output)
{
    char buf[500];
    while(fgets(buf, sizeof(buf), input))
    {
        //strip end-of-line from `buf`
        if(strlen(buf))
            if(buf[strlen(buf) - 1] == '\n')
                buf[strlen(buf) - 1] = 0;

        //tokenize with strtok
        char *token = strtok(buf, ",");
        while(token)
        { 
            fprintf(output, "%s", token);
            token = strtok(NULL, ",");
        }
        fprintf(output, "\n");
    }
}

int main(int argc, char *argv[])
{
    FILE *input = fopen("input.txt", "r");
    FILE *output = fopen("output.txt", "w");
    store(input, output);
    return 0;
}
int size = 1000;
data *d;
d = (data*)malloc(size*sizeof(data)); 
alloc_data(d, size);

Need help resolving fscanf and fprintf question


By : Oleg
Date : March 29 2020, 07:55 AM
With these it helps My program is supposed to read 4 numbers from a .txt file, add 10 to the numbers, then print it back. I have a function which loads the text file with four numbers, then another function which adds 10 and appends the file. The program currently works, but I am very confused within my addTen() function. ,
The program currently works
code :
void addTen(int array[], int size) {
    for (int i=0; i < size; i++) {   
        array[i] += 10;
    }
}

void loadTextFile(FILE *file, int array[], int size) {
    for (int i=0; i < size; i++) {   
        fscanf(file, "%d", &array[i]);
    }
}
void printArray(FILE* fp, int array[], int size) {
    for (int i=0; i < size; i++) {   
        fprintf("%d\n", array[i]);
    }
}
int array[4];
loadTextFile(text, array, 4);
addTen(array, 4);
printArray(text, array, 4);
Related Posts Related Posts :
  • Importance of '0' character and << operator in this program
  • How to debug a quadratic probing implementation for hash tables in C without using pointers?
  • Pointer to Pointer in c, getting unexpected value
  • "Permission denied" in open() function in C
  • How to assign an array of structs
  • Sort an Array of struct by a value of a field inside the struct
  • Implement stack with pointers in c
  • C program that goes through a string and flags for non-letters
  • C while statement in switch clause
  • Can we allocate memory for char * but return it as const char *?
  • How do Comma-separated expressions as condition in a for-loop work?
  • Sort Structs by the value of an attribute in it
  • How should I make users to put in the info that I want?
  • Are bitmask checks more efficient than comparing numbers?
  • Are the fast integer types faster when stored in the CPU registers?
  • Does realloc deallocate before trying to allocate new memory?
  • Failing to compile a 15+ year old MUD codebase written in C
  • opendir dont get content of buffer char * on C language?
  • Can we have '+' operator inside printf()?
  • Why scanf function do not accept escape sequences as an input?
  • Print strings of length of k from a and b
  • C problem with passing pointer to struct to function
  • Shall I use register class variables in modern C programs?
  • Can I specify conditions for the rand() function?
  • Fast formula to get the range a number is in, given a perfect binary subdivision?
  • Why can't I use more than two registers in inline asm?
  • A question of strstr() and fgets() in a demo from the book《Head First C》
  • the problem is from a algorithm problem I do recently,but I can't gain the right answer
  • Problem to access members in pointer struct located in another pointer struct
  • Check that all symbols defined in static library
  • Function will only properly work if I've printed the values of the attributes
  • Why my code is showing time limit exceeded
  • #define statement for address of memory
  • How to call a function from another .c file
  • Memory layout of C struct
  • Why should we use return statement when using a recursive call?
  • error: expected identifier or '(' before '=' token
  • Why is it not possible to exchange the position of array name and index in swift like C (a[i] == i[a])?
  • How can I allocate enough memory to store an entire text file in C?
  • Segmentation fault when I use afl fuzzer
  • What happens in the assembly output when we add "cc" to clobber list
  • What is the equivalent of Keil _at_ in gcc
  • Simple question about declaring an empty string in C
  • Heap-Overflow or Buffer-Overflow?
  • C: Why is &= better that =?
  • Create exactly 5 process with fork()
  • gcc compile function in ifdef
  • where is the memory leak in the code below?
  • Most efficient formula for unpacking 16-bit BCD? (e.g. 0x1234 to 0x01020304)
  • how to find which integer no is maximim out of given numbers
  • Difference between a VOID function with a string argument VS a function with a string argument that returns a char array
  • why the output of the following program different (random value) every time?
  • Why does this macro expression in C behave unexpectedly?
  • When is memory allocated and de-allocated static and dynamic memory in C?
  • In C, what happens if we left shift the bits out of range and again right shift the values in the same operation
  • why the following c program is giving segmentation error?
  • How can I run through all the characters of a string and check if they're all numbers in C?
  • adc frequency vs accuracy
  • why the output of following c program is 16 16 16?
  • Is it possible to declare a function in header file with unknown type that is specified only in file '.c'?
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk