| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1151 人关注过本帖
标题:error?
只看楼主 加入收藏
cstdio
Rank: 5Rank: 5
来 自:上海市静安区
等 级:贵宾
威 望:15
帖 子:97
专家分:44
注 册:2018-5-30
结帖率:87.5%
收藏
已结贴  问题点数:2 回复次数:2 
error?
出了点错误(该程序有C语言成分)
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
//string filename;
int file();
int file2();
int main(){
    std::string choise;
    while(1){
    std::cout<<"write file or read file?"<<std::endl;
   
    scanf("%s",&choise);
    if(choise=="write "){
        file();
    }
    else
    file2();
}
    return 0;
}
int file(){
    FILE *fp;
    std::string filename;
    int i;
    std::cout<<"enter the filename you want to write\n";
   
    //for(i=0;i<8;i++)
    scanf("%s",&filename);
    fp=fopen(filename,"w");//error
    std::cout<<"enter the string that you want to write into the file\n";
    std::string writefile;
    scanf("%s",&writefile);
    fprintf(fp,"%s\n",writefile);
    printf("finish write file,file will close and back to main()\n");
    fclose(fp);
    return 0;
    //main();
}
int file2(){
    FILE *fp;
    std::string filename;
    std::string files;
    scanf("%s",&filename);
    fp=fopen(filename,"r");//error
    printf("in %s ,things in the file is\n",filename);
    fscanf(fp,"%s",&files);
    printf("%s\n",files);
    fclose(fp);
    printf("finish read file,file will close and back to main()\n");
    return 0;
}
搜索更多相关主题的帖子: std string file int write 
2018-07-13 11:54
Jonny0201
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:52
帖 子:488
专家分:2603
注 册:2016-11-7
收藏
得分:2 
要么都用 C++
要么都不用
文件读写 C++ 有 fstream, 不需要用 C 的函数, 并且用 C 的函数要使用 C++ 标准库提供的与 C 交互的部分
scanf 我记得没有 %s, C 语言里对字符串的读写我记得使用 get 和 put
string 不能直接传给 C 的函数, C 里面接受的基本都是 const char *, string 有提供与 C 风格字符串交互的函数 : string::c_str()
你这样的程序我建议干脆不要用 C++, 名字的输入直接用 char *, 这样就可以直接传进去了
我根据编译错误随便改了一下, 还有一个警告我就不管了, 程序逻辑是否有错误自己纠正
程序代码:
#include<iostream>
#include<stdio.h>
#include<string>
using namespace std;
//string filename;
int file();
int file2();
int main(){
    std::string choise;
    while(1){
    std::cout<<"write file or read file?"<<std::endl;
    
    cin >> choise;
    if(choise=="write "){
        file();
    }
    else
    file2();
}
    return 0;
}
int file(){
    FILE *fp;
    std::string filename;
    int i;
    std::cout<<"enter the filename you want to write\n";
    
    //for(i=0;i<8;i++)
    cin >> filename;
    fp=fopen(filename.c_str(),"w");//error
    std::cout<<"enter the string that you want to write into the file\n";
    std::string writefile;
    cin >> writefile;
    fprintf(fp,"%s\n",writefile.c_str());
    printf("finish write file,file will close and back to main()\n");
    fclose(fp);
    return 0;
    //main();
}
int file2(){
    FILE *fp;
    std::string filename;
    std::string files;
    cin >> filename;
    fp=fopen(filename.c_str(),"r");//error
    printf("in %s ,things in the file is\n",filename.c_str());
    fscanf(fp,"%s",files.c_str());
    printf("%s\n",files.c_str());
    fclose(fp);
    printf("finish read file,file will close and back to main()\n");
    return 0;
}
2018-07-13 12:16
cstdio
Rank: 5Rank: 5
来 自:上海市静安区
等 级:贵宾
威 望:15
帖 子:97
专家分:44
注 册:2018-5-30
收藏
得分:0 
谢谢

import random
i=random.randint(100,100000)
print i
2018-07-13 14:59
快速回复:error?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019218 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved