fscanf()在VSTS中的文件处理中未获取数据
fscanf() is not getting the data in file handling in VSTS
嗨,我有以下代码
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
FILE *fp;
char text[5];
int age;
fp=fopen("E:Text1.txt","w+");
printf("Enter text here and Enter Age :");
scanf("%s %d",text,&age);
fprintf(fp,"%s %d",text,age);
printf("Entered Text and Age is :n");
fscanf(fp,"%s %d",text,&age);
printf("Text=%s Age=%d",text,age);
fclose(fp);
getche();
}
我对fscanf函数有问题。数据没有显示在输出中。当我尝试调试代码时,它会抛出一个错误,说明"未处理的异常…访问冲突"在这个代码
fscanf(fp,"%s %d",text,&age);
据我所知,我认为VSTS无法获取文件位置。我已经在E驱动器中创建了文件。请帮我解决我的问题。
有两个问题
第一条是路径"E:Text1.txt"=>"E:\Text1.txt"
秒是fopen("E:\Text1.txt","w+");
打开该文件写入
如果你想从该文件中读取,那么你应该打开它,用读取
fp=fopen("E:\Text1.txt","r");
因为你用w+
打开了它,所以你可以使用
fseek(fp,0,0);
指向文件的开头
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
FILE *fp;
char text[5];
int age;
fp=fopen("E:\Text1.txt","w+");
printf("Enter text here and Enter Age :");
scanf("%s %d",text,&age);
fprintf(fp,"%s %d",text,age);
fclose(fp);
fp=fopen("E:\Text1.txt","r");
printf("Entered Text and Age is :n");
fscanf(fp,"%s %d",text,&age);
printf("Text=%s Age=%d",text,age);
fclose(fp);
getche();
}
另一个版本是
# include <stdio.h>
# include <conio.h>
# include <process.h>
void main()
{
FILE *fp;
char text[5];
int age;
fp=fopen("E:\Text1.txt","w+");
printf("Enter text here and Enter Age :");
scanf("%s %d",text,&age);
fprintf(fp,"%s %d",text,age);
fseek(fp,0,0);
printf("Entered Text and Age is :n");
fscanf(fp,"%s %d",text,&age);
printf("Text=%s Age=%d",text,age);
fclose(fp);
getche();
}
您的文件路径格式不正确,您应该对其进行双反斜杠:
fp=fopen("E:\Text1.txt","w+");
编辑:经过检查,我意识到你的问题实际上应该与位置指示器有关,一旦你写了,它就设置在文件的末尾,你应该倒带它:
fprintf(fp,"%s %d",text,age);
rewind(fp);
相关文章:
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 如何在 c++ 窗口中将参数和返回的退出值传递到批处理文件/从批处理文件获取返回的退出值
- 从处理程序捕获时如何获取 Ada 异常消息C++?
- 如何处理在 bash 文件中获取运行时输入的 c++ 程序
- 从后台处理程序获取作业 - C++
- 获取特定监视器的处理程序以在其中放置弹出窗口
- 使用非静态消息处理程序获取GSTREAMER总线消息
- 无法获取程序来处理WM_PowerBroadcast消息
- 如何获取函数以在 Main() 中重新处理变量
- 从 exe 获取输出C++并在 Python 中实时处理它
- 使用Arduino Due从MPU-6050获取/处理正确的值U
- 如何在自定义信号处理程序中以编程方式获取sigterm的默认行为
- C++ Opencv - 通过套接字获取后的图像处理
- 如何获取对话框的处理程序(HWND)
- 处理POSIX信号:如何在不使用BackTrace_symbols的情况下获取堆栈跟踪
- 有没有一种方法可以获取在处理信号期间访问的指针
- 最有可能使用的方法是处理多个键以使用STL容器获取值
- 在数据流处理程序中获取segfault
- MFC C++正在从WM_KEYDOWN处理程序获取在OnDraw()中编辑的CDC pDC的句柄
- 获取 std::vector 的大小(通过扩展专门使用参数类型的可变参数模板函数来处理参数)