我正在写这段代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int i;
fp = fopen("keimeno.txt","r");
fscanf(fp,"%d",i);
printf("%d\n",i);
fclose(fp);
return 0;
}
,文件包含:
2
Yiannis Ioannou 356
3
Today
10347
If
345
And then none
1542
John Smith 743
2
My story
3940
Feedback
682
END
当我尝试运行它时,它会退出值3221225477
而不是打印数字2 ..
任何人都可以解释原因吗?
答案 0 :(得分:8)
扫描数字时,需要传递要存储结果的变量的地址:
fscanf(fp,"%d",&i);
你有
的地方fscanf(fp,"%d",i);
^ missing the & sign!
您的编译器真的应该警告过您 - 编译时是否启用了警告?
这里发生的是fscanf
函数写入给定的位置(在您的情况下,它通过值指向的任何位置) > i
,而不是写入i
的位置。这可能会以各种令人讨厌的方式破坏你的记忆 - 在你的情况下,在程序中运行&#34;运行&#34;在崩溃前相当长的时间。
正如@Brandin指出的那样,你的代码还有一个问题(虽然它不太可能成为你问题的根源)。当您尝试打开文件时,您应该始终检查您是否成功。你可以这样做:
#include <assert.h>
// at the top of the program
// attempt to open the file:
fp = fopen("keimeno.txt","r");
// and check whether you succeeded:
assert(fp != NULL); // this says "check fp is not NULL. Otherwise, quit."
或者,您可以通过以下方式使事情变得更漂亮:
const char *fileName = "keimeno.txt";
const char *mode = "r";
if((fp=fopen(fileName, mode))==NULL) {
printf("cannot open file %s\n", fileName);
return -1;
}
放置&#34;硬连线值&#34;几乎总是一个好主意。在程序开头附近,而不是将它们嵌入到函数调用中。
答案 1 :(得分:0)
与我的问题相同。我清空列表,并添加了一个新值,该值导致我用另一个值输入了该值,并以错误“ process-exited-with-return-value-3221225477”结束
#include <iostream>
#include <stdlib.h>
using namespace std;
struct Node {
int data;
struct Node *left;
struct Node *right;
};
int count = 0;
struct Node*front = NULL;
struct Node*rear = NULL;
void enqueue(int val) {
count++;
struct Node* newnode = new Node; //= (struct Node*) malloc(sizeof(struct Node));
newnode->data = val;
if (front == NULL) {
front = rear = newnode;
newnode->right=newnode->left;
newnode->left=newnode->right;
}
else {
rear->right = newnode;
newnode->left = rear;
rear = newnode;
front->left = rear;
rear->right = front;
}
}
void dequeue() {
if(front==NULL)
cout<<"There is no element yet. Cannot be dequeue."<<endl;
else {
if (front == rear) {
cout<<"The dequeued element is "<< front->data <<endl;
front = rear = NULL;
delete front;
}
else {
cout<<"The dequeued element is "<< front->data <<endl;
struct Node *curr = front;
front = front->right;
front->left = rear;
rear->right = front;
delete curr;
}
}
}
void display() {
int i;
struct Node* ptr;
if(front==NULL) {
cout<<"The List is empty, nothing to display.";
cout<<endl;
return;
}
ptr = front;
cout<<"Element/s : ";
for (i=0;i<count; i++) {
cout<< ptr->data <<" ";
ptr = ptr->right;
}
if (ptr->right==rear) {
cout<< ptr->data << " ";
}
cout<<endl;
}
int main() {
int ch, val;
cout<<"1) Enqueue"<<endl;
cout<<"2) Dequeue"<<endl;
cout<<"3) Display"<<endl;
cout<<"4) Exit"<<endl;
do {
cout<<"Enter your choice: "<<endl;
cin>>ch;
switch(ch) {
case 1: {
cout<<"Enter the value of the element:"<<endl;
cin>>val;
enqueue(val);
cout<<endl;
return main();
break;
}
case 2: {
dequeue();
cout<<endl;
return main();
break;
}
case 3: {
display();
cout<<endl;
return main();
break;
}
case 4: {
cout<<"Exit"<<endl;
break;
}
default: {
cout<<"Invalid Choice"<<endl;
}
}
}while(ch!=4);
return 0;
}
答案 2 :(得分:-1)
exited_return_value-3221225477是一个高度抽象的代码错误放错位置 编译器由于各自编译器的gnu逻辑错位而产生相同的代码 在另一个系统中执行。超过该值称为系统call()错误...