C++代码报错

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++代码报错相关的知识,希望对你有一定的参考价值。

#include<iostream>
using namespace std;
FILE *stream,*stream1,stream2;
void main(void)

int numclosed;
char list[30];
int i,numread,numwritten;
if( (stream1 = fopen( "date","r")) == NULL)
printf("打开文件‘date’读失败\n");
else printf("打开文件‘date’进行读\n");
if( (stream2 = fopen( "date2","w+")) == NULL)
printf("打开文件‘date2’写失败\n");
else printf("打开文件‘date2’进行写\n");
if( (stream = fopen("fread.out","w+r"))!=NULL)

for ( i = 0; i < 25; i++)
list[i] = (char)('z' - i);
numwritten = fwrite( list,sizeof( char ), 25, stream);
printf( "写入%d个字符\n,numwritten");
fclose( stream);

else printf( "打开文件fread.out时发生错误,无法写数据到文件中\n" );
if( ( stream = fopen( "fread.out", "r+t" )) !=NULL)

numread = fread( list, sizeof( char ), 25, stream );
printf( "读取的数据个数 = %d\n", numread );
printf( "读取的内容为 = %.25s\n", list);
fclose( stream );

else printf( "打开文件fread.out时发生错误,无法从文件中读取数据\n" );
if( fclose( stream1 ))
printf( "关闭文件data失败\n" );
numclosed = _fcloseall( );
printf( "使用函数_fcloseall关闭的文件数目为 :%u\n", numclosed );


报错
1>d:\我的程序\ceshi\ceshi\文件练习.cpp(9): warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
1> d:\vs2010\vc\include\stdio.h(234) : 参见“fopen”的声明
1>d:\我的程序\ceshi\ceshi\文件练习.cpp(12): error C2679: 二进制“=”: 没有找到接受“FILE *”类型的右操作数的运算符(或没有可接受的转换)
1> d:\vs2010\vc\include\stdio.h(65): 可能是“_iobuf &_iobuf::operator =(const _iobuf &)”
求解

//FILE *stream,*stream1,stream2;
这个的stream2少写个*
FILE *stream,*stream1,*stream2;
参考技术A stream2前面少了* 参考技术B #include<iostream>
using namespace std;
FILE *stream,*stream1,*stream2; //stream2必须是指针
void main(void)

int numclosed;
char list[30];
int i,numread,numwritten;
if( (stream1 = fopen( "date","r")) == NULL)
printf("打开文件‘date’读失败\n");
else printf("打开文件‘date’进行读\n");
if( (stream2 = fopen( "date2","w+")) == NULL) //这里报错
printf("打开文件‘date2’写失败\n");
else printf("打开文件‘date2’进行写\n");
if( (stream = fopen("fread.out","w+r"))!=NULL)

for ( i = 0; i < 25; i++)
list[i] = (char)('z' - i);
numwritten = fwrite( list,sizeof( char ), 25, stream);
printf( "写入%d个字符\n,numwritten");
fclose( stream);

else printf( "打开文件fread.out时发生错误,无法写数据到文件中\n" );
if( ( stream = fopen( "fread.out", "r+t" )) !=NULL)

numread = fread( list, sizeof( char ), 25, stream );
printf( "读取的数据个数 = %d\n", numread );
printf( "读取的内容为 = %.25s\n", list);
fclose( stream );

else printf( "打开文件fread.out时发生错误,无法从文件中读取数据\n" );
if( fclose( stream1 ))
printf( "关闭文件data失败\n" );
numclosed = _fcloseall( );
printf( "使用函数_fcloseall关闭的文件数目为 :%u\n", numclosed );

运行C++程序报错:Segmentation fault(coredump) 请问为啥?

bool find(string str,ifstream &f)

f.seekg(0,std::ios_base::beg);
while(getline(f,str))

return true;

return false;


int main(int argc, char* argv[])

ifstream f("a.txt");
for(int i = 0;i < 100;++i)
if(!find(s,f))




f.close();
return 0;

拜托,公司代码不能随便贴的,这是原则啊。

你删代码了?怎么没定义s

这个错误一般是因为越界的问题,需要跟踪调试,比如由代码段跳到了数据段等
参考技术A 问个问题都遮遮掩掩,你的代码送给别人别人还不要呢。

以上是关于C++代码报错的主要内容,如果未能解决你的问题,请参考以下文章

错误记录Visual Studio 编译 C++ 代码报错 ( To disable deprecation, use _CRT_SECURE_NO_WARNINGS. )

android编译surface c++程序时,报错缺少.o文件

C++ 编译运行报错 error: stray ‘200’ in program 解决方案

rcpp报错,在mac上学习Rcpp和C++

C++编译报错:error: expected constructor, destructor, or type conversion before ‘(’ token

运行C++程序报错:Segmentation fault(coredump) 请问为啥?