Question

I posted a few days ago already about a problem with my array-program. Well, it is basically a program which lets the user generate an array, safe a specific number in a specific slot, read the array or read a specific slots value. Now I am figuring out, how to let the user save the current array as a file.

this is what I got

void safeFile(){
    FILE *f = fopen("list.txt", "a+");
    putc(f , list);
    fclose(f);
    printf("File saved");
    start();
    }

where's the problem?

My Program crashes everytime I call the function safeFile() .

I played around, came with fputs instead of putc, program won't crash anymore, the file gets created but it's still blank

void safeFile(){
    FILE *f = fopen("list.txt", "r+");
    fputs(f , list);
    fclose(f);
    printf("File saved");
    start();
    }

here is my current full code

would be grateful for advices, im a newb

#include <stdio.h>
#include <stdlib.h>

int list[30];
int x = 0;
int i = 0; // variable used by for-Loop in setList
int j = 0; // variable used by for-Loop in getList
int input;
int option; //start Value
int gvinput; //getValue input
void start();
void getList();

int main()
{
    start();
    return 0;
}

void setList(int sizeOfList)
{
    for (i = x; i <= sizeOfList; i++)
    {

        list[i] = i;

    }

}
void getList(){
    for(j = x; j < i ; j++ )
    {
        printf("At %d we got the value %d \n",j,list[j]);
    }
}

void startList()
{
    fflush(stdin);
    printf("Please enter number between 0 and 30\n ");
    scanf("%d",&input);
    if(input > 30 || input == 0)
    {
        printf("The Number is not between 0 and 30\n");
        startList();
    }
    setList(input);
    getList();
    fflush(stdin);
    start();
}

void setValues(int l[])
{
    fflush(stdin);
    int v;
    int loc;
    printf("please enter what value you want to safe\n");
    scanf("%d",&v);
    fflush(stdin);
    printf("Where do you want to save it?\n");
    scanf("%d",&loc);
    l[loc] = v;
    printf("we got at slot %d the value %d\n\n",loc,l[loc]);
    start();
}

void getValues(int getArray[]){

fflush(stdin);

printf("which slot do you want to read?\n");
scanf("%d",&gvinput);
fflush(stdin);
printf("The value is: %d \n\n",getArray[gvinput]);
start();
}
void safeFile(){
    FILE *f = fopen("list.txt", "r+");
    fputs(f , list);
    fclose(f);
    printf("File saved");
    start();
    }

void start(){
    fflush(stdin);
    printf("\n");
    printf("[L] = generate Slots\n");
    printf("[S] = set a Value at specific slot\n");
    printf("[G] = get a Value from a specific slot\n");
    printf("[F] = safe File");
    printf("[X] = exit\n");

    option=getchar();
    if(option == 'L' || option == 'l'){
        startList();
    }
    if(option == 'S' ||option == 's'){
        setValues(list);
    }
    if (option =='G' ||option == 'g'){
        getValues(list);
     }
     if (option == 'X' || option == 'x'){
        printf("Thank you");
     }
     if (option == 'f' || option == 'F'){
        safeFile();
     }
Was it helpful?

Solution

int putc( int ch, std::FILE* stream );

putc writes a single char, and you pass an array of ints to it. The same is true for fputs, which writes a null-terminated string. You'll really have to write some code that serializes your data structure to a sequence of bytes to be written to the file.

In your case, since you want to save a list of ints, you can do this with a loop, for example, that is, loop over your array and write each item to the file. You'll also need a function that writes an int (putc isn't good here, since it writes a char). Have a look at printf, if you'd like to stick with C-style IO, otherwise use streams.

OTHER TIPS

If your using putc(), you should be passing char by char.

for(i=0;i<strlen(list);i++)
{
  putc(f,list[i]);
}

The list is an array, if you pass the address it means whole array so putc won't work here.

int fputs(const char *s, FILE *stream);

The second argument to the fputs is the stream. Since u use int [10]. u can use fprintf to print to the file.

Also, fflush(stdin); is undefined behaviour

you need to use the loop, but it is not working because the parameters in the putc function are not in the correct order. Try this:

for(i=0;i<strlen(list);i++)
{
  putc(list[i],f); //<---------- NOT putc(f,list[i]);
}

first the char and second the stream.

int putc ( int character, FILE * stream );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top