[英]saving array to a file won't work, program crashes
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() . 每当我调用函数safeFile()时,我的程序就会崩溃。
I played around, came with fputs instead of putc, program won't crash anymore, the file gets created but it's still blank 我玩了,用fputs代替了putc,程序不再崩溃,文件被创建,但是仍然空白
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();
}
int putc( int ch, std::FILE* stream );
putc
writes a single char
, and you pass an array of int
s to it. putc
编写一个char
,然后将一个int
数组传递给它。 The same is true for fputs
, which writes a null-terminated string. fputs
也是如此,它会写一个以空值结尾的字符串。 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 int
s, you can do this with a loop, for example, that is, loop over your array and write each item to the file. 在您的情况下,由于要保存一个
int
列表,因此可以使用循环进行此操作,例如,循环遍历数组并将每个项目写入文件。 You'll also need a function that writes an int
( putc
isn't good here, since it writes a char
). 您还需要一个编写
int
的函数( putc
在这里不好,因为它编写了char
)。 Have a look at printf
, if you'd like to stick with C-style IO, otherwise use streams . 如果您想使用C风格的IO,请看看
printf
,否则请使用stream 。
If your using putc(), you should be passing char by char. 如果使用putc(),则应逐个字符传递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. 该列表是一个数组,如果您传递地址,则意味着整个数组,因此putc在这里不起作用。
int fputs(const char *s, FILE *stream);
The second argument to the fputs
is the stream. fputs
的第二个参数是流。 Since u use int [10]
. 由于您使用
int [10]
。 u can use fprintf
to print to the file. 您可以使用
fprintf
打印到文件。
Also, fflush(stdin);
另外,
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. 您需要使用循环,但是它不起作用,因为putc函数中的参数顺序不正确。 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 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.