Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/59.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 无法从文件中读取_C++_C_File Io - Fatal编程技术网

C++ 无法从文件中读取

C++ 无法从文件中读取,c++,c,file-io,C++,C,File Io,我已经编写了一个代码,可以读取名为network.dat的文件 我写的代码是 f = fopen("network.dat", "r"); if(f == NULL) exit(1); int read, N; printf("%p\n", f);//output file pointer, included this just to check if file is opened properly fscanf(f, "%d%d", &

我已经编写了一个代码,可以读取名为network.dat的文件

我写的代码是

    f = fopen("network.dat", "r");
    if(f == NULL)
        exit(1);
    int read, N;

    printf("%p\n", f);//output file pointer, included this just to check if file is opened properly
    fscanf(f, "%d%d", &N, &read);//error here
    cout<<N; 

等等。我只关注文件中的前两个数字,即10和1。

您的代码要求文件中的所有字符直到第一个空格都是int。如果文件不是以int开头的,这可能是失败的原因。

您的scanf()格式字符串不正确。“%d,%d”查找由逗号分隔的两个整数。如果要读取由空格分隔的两个整数,只需执行“%d%d”。

正如我在中所述,问题在于格式说明符不正确。试一试

fscanf(f, "%d%d", &N, &read);

既然你使用了<代码> CUT<代码>,我正在猜测这是C++代码…老实说,你真的应该而不是以规范的方式来做这件事。相反,使用

std::ifstream输入(“network.dat”);
int N,读取;
输入>>N>>读取;

std::cout这似乎有效。代码是一个快速而肮脏的剪切和粘贴作业,样式为零,但它将此作业作为一个测试。记录中的字段数似乎需要与打印格式字符串中的字段相匹配。我在1.9999的记录1中的测试数据中添加了第三个字段,它成功了。我怀疑这是一个技术上纯粹的解释

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::ios;

int main(int argc, char *argv[])
{
//int read;
//int N;
int res;
FILE *f;


f = fopen("network.dat", "r");
    if(f == NULL)
        exit(1);
    int read, N;
    float f3;

    printf("%p\n", f);//output file pointer, included this just to check if file is opened properly
    for (;;)
        {
    res = fscanf(f, "%d%d%f", &N, &read, &f3);//error here
    if (res <= 0)
        {
        printf("err %d\n",errno);
        break;
        }
    cout<<N << " " << read << "\n";
        }
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用std::cout;
使用std::endl;
使用std::cin;
使用std::ios;
int main(int argc,char*argv[])
{
//int-read;
//int N;
国际关系;
文件*f;
f=fopen(“网络数据”,“r”);
如果(f==NULL)
出口(1);
整数读取,N;
浮动f3;
printf(“%p\n”,f);//输出文件指针,包含此指针只是为了检查文件是否正确打开
对于(;;)
{
res=fscanf(f,“%d%d%f”,&N,&read,&f3);//此处出错

如果(res BTW,您应该使用
%p
打印
f
,而不是
%d
。@all,我已经按照您的意愿更正了代码段。现在有一个
N
。但这不是问题所在。@Srijan,问题出在哪里?您确定您的文件的格式是int,int?所以是3,5或6,8?如果不是,例如,如果有额外的spa)ce在逗号之后,它将失败!您需要显示network.dat的内容。这是一个打字错误,我已经更正了。感谢您指出它。如果您发现任何scanf()函数都没有达到预期效果,请首先检查格式字符串是否正确,然后检查您是否正在传递指针(您在这里)这是一个排版,我已经纠正了。谢谢你指出。@ Srijan,你能告诉我们什么是“错误”吗?你有什么问题?你能发布一个?@ SRijan我强烈建议你使用C++替代品。
std::ifstream input("network.dat");
int N, read;
input >> N >> read;
std::cout << N << ' ' << read << std::endl;
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <cstring>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;
using std::cin;
using std::ios;

int main(int argc, char *argv[])
{
//int read;
//int N;
int res;
FILE *f;


f = fopen("network.dat", "r");
    if(f == NULL)
        exit(1);
    int read, N;
    float f3;

    printf("%p\n", f);//output file pointer, included this just to check if file is opened properly
    for (;;)
        {
    res = fscanf(f, "%d%d%f", &N, &read, &f3);//error here
    if (res <= 0)
        {
        printf("err %d\n",errno);
        break;
        }
    cout<<N << " " << read << "\n";
        }
}