Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.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++ 使用sscanf从文件C++; #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { IFIFile; char fName[30][30]; long int uTime[30][10]; ifile.open(“sync.log”); char ch; 字符串str=“”; if(ifile.fail()) { 考虑到你有: #include "stdafx.h" #include <fstream> #include <iostream> #include <string> #include <conio.h> using namespace std; int main(int argc, char* argv[]) { ifstream ifile; char fName[30][30]; long int uTime[30][10]; ifile.open("sync.log"); char ch; string str = ""; if(ifile.fail()) { cout<<"Invalid File Name!"; system("pause"); exit(1); } int i = 0; while(!ifile.eof()) { getline(ifile, str); cout<<str<<endl; sscanf(str.c_str(),"%ld %[^\n]",&uTime[i],fName[i]); i++; str = ""; } ifile.close(); system("pause"); cout<<"Output:"<<endl; for(i = 0 ; i < 2 ; i ++) { cout<<uTime[i]<<" "; cout<<fName[i]; cout<<endl; } getch(); return 0;_C++_File_Scanf - Fatal编程技术网

C++ 使用sscanf从文件C++; #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { IFIFile; char fName[30][30]; long int uTime[30][10]; ifile.open(“sync.log”); char ch; 字符串str=“”; if(ifile.fail()) { 考虑到你有: #include "stdafx.h" #include <fstream> #include <iostream> #include <string> #include <conio.h> using namespace std; int main(int argc, char* argv[]) { ifstream ifile; char fName[30][30]; long int uTime[30][10]; ifile.open("sync.log"); char ch; string str = ""; if(ifile.fail()) { cout<<"Invalid File Name!"; system("pause"); exit(1); } int i = 0; while(!ifile.eof()) { getline(ifile, str); cout<<str<<endl; sscanf(str.c_str(),"%ld %[^\n]",&uTime[i],fName[i]); i++; str = ""; } ifile.close(); system("pause"); cout<<"Output:"<<endl; for(i = 0 ; i < 2 ; i ++) { cout<<uTime[i]<<" "; cout<<fName[i]; cout<<endl; } getch(); return 0;

C++ 使用sscanf从文件C++; #包括“stdafx.h” #包括 #包括 #包括 #包括 使用名称空间std; int main(int argc,char*argv[]) { IFIFile; char fName[30][30]; long int uTime[30][10]; ifile.open(“sync.log”); char ch; 字符串str=“”; if(ifile.fail()) { 考虑到你有: #include "stdafx.h" #include <fstream> #include <iostream> #include <string> #include <conio.h> using namespace std; int main(int argc, char* argv[]) { ifstream ifile; char fName[30][30]; long int uTime[30][10]; ifile.open("sync.log"); char ch; string str = ""; if(ifile.fail()) { cout<<"Invalid File Name!"; system("pause"); exit(1); } int i = 0; while(!ifile.eof()) { getline(ifile, str); cout<<str<<endl; sscanf(str.c_str(),"%ld %[^\n]",&uTime[i],fName[i]); i++; str = ""; } ifile.close(); system("pause"); cout<<"Output:"<<endl; for(i = 0 ; i < 2 ; i ++) { cout<<uTime[i]<<" "; cout<<fName[i]; cout<<endl; } getch(); return 0;,c++,file,scanf,C++,File,Scanf,当您这样做时: long int uTime[30][10]; cout我想你是想用: cout << uTime[i] << … 而不是 long int uTime[30]; 因此,将数据读入uTime的行和将uTime写入cout的行都是有意义的。您没有显示输出文件的写入位置。考虑到问题与输出有关,该代码可能是相关的。您的问题毫无意义。怎么可能如果文本文件包含十进制值,“从文件中获取十六进制值”。您可能正在将其打印为十六进制。请向我们显示您输出uTime[i]

当您这样做时:

long int uTime[30][10];

cout我想你是想用:

cout << uTime[i] << …
而不是

long int uTime[30];

因此,将数据读入
uTime
的行和将
uTime
写入
cout
的行都是有意义的。

您没有显示输出文件的写入位置。考虑到问题与输出有关,该代码可能是相关的。您的问题毫无意义。怎么可能如果文本文件包含十进制值,“从文件中获取十六进制值”。您可能正在将其打印为十六进制。请向我们显示您输出
uTime[i]的代码
@BillXia查看我发布的代码..我不明白。你在找
std::hex
IO操纵器吗?我使用uTime 2D是因为我想存储文件中的多个值…请看上面我的问题,我已经编辑了它现在很清楚..你已经编写了
uTime[0]=1399865017;
但该赋值不合理-我很惊讶它会编译(它不会为我编译,因为我告诉编译器将警告视为错误)。您需要指定
uTime[0][0]=1399865017;
来分配给2D数组的一个元素。在
sscanf()中
在你的
cout ok谢谢修复了我刚刚更改了long int uTime[10];我很愚蠢为什么我使用2d数组我看不出来,无论如何再次感谢:)既然你有30个名称的空间(每个30个字符),你不也需要30个
uTime
值吗?
long int uTime[30][10];