[英]Process exited with return value 3221225477
i am writing this code:我正在写这段代码:
#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;
}
and the file contains:该文件包含:
2
Yiannis Ioannou 356
3
Today
10347
If
345
And then none
1542
John Smith 743
2
My story
3940
Feedback
682
END
When I try to run it, it exits me value 3221225477
instead of printing the number 2..当我尝试运行它时,它会退出我的值
3221225477
而不是打印数字 2..
Can anyone explain why?谁能解释为什么?
When you scan a number, you need to pass the address of the variable where you want to store the result: 扫描数字时,需要在要存储结果的地方传递变量的地址:
fscanf(fp,"%d",&i);
where you have 你在哪里
fscanf(fp,"%d",i);
^ missing the & sign!
Your compiler really ought to have warned you - do you enable warnings when you compile? 您的编译器确实应该发出警告-编译时是否启用警告?
What is happening here is that the fscanf
function writes to the location given (in your case, it writes to whatever location is pointed to by the value of i
, instead of writing to the location of i
) . 这里发生的是
fscanf
函数写入给定的位置(在您的情况下,它写入i
值 指向的任何位置,而不是写入i
的位置 )。 This can corrupt your memory in all kinds of nasty ways - resulting, in your case, in the program "running" for considerable time before crashing. 这会以各种令人讨厌的方式破坏您的内存-在您的情况下,导致程序“运行”相当长的时间才崩溃。
As @Brandin pointed out, there is a further problem with your code (although it's less likely to be the source of your problem). 正如@Brandin指出的那样,您的代码还有另一个问题(尽管不太可能成为问题的根源)。 When you attempt to open a file, you should ALWAYS check that you succeeded.
尝试打开文件时,应始终检查是否成功。 You do this with something like this:
您可以使用以下方法执行此操作:
#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."
Alternatively, you can make things a bit prettier with: 另外,您可以使用以下方法使事情更漂亮:
const char *fileName = "keimeno.txt";
const char *mode = "r";
if((fp=fopen(fileName, mode))==NULL) {
printf("cannot open file %s\n", fileName);
return -1;
}
It is almost always a good idea to put "hard wired values" near the start of your program, rather than embedding them in a function call. 将“硬接线值”放在程序的开头几乎总是一个好主意,而不是将其嵌入函数调用中。
Same of my problem. 我的问题也一样。 I empty my list and added a new value the it resulted to the value I've entered with another values and ended with an error "process-exited-with-return-value-3221225477"
我清空列表,并添加了一个新值,该值导致我使用其他值输入的值并以错误“ 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;
}
Mate, I am so grateful for posting this.伙计,我非常感谢您发布此消息。 I haven't used scanf for a while and it saved me from pointlessly wasting my time trying to figure out where the problem could be and step-by-step debugging.
我已经有一段时间没有使用 scanf 了,它让我免于浪费时间试图找出问题所在并逐步调试。 TY so much!
太棒了! It is not even the first time I am visiting that blog
这甚至不是我第一次访问那个博客
exited_return_value-3221225477是由于各个编译器的gnu逻辑错位导致的编译器代码错误的高度抽象错位。同一代码将在另一个系统中执行。该值称为系统调用()错误...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.