https://stackoverflow.com/questions/3081289/how-to-read-a-line-from-a-text-file-in-c-c
https://stackoverflow.com/questions/7868936/read-file-line-by-line
http://www.cplusplus.com/reference/cstdio/fscanf/
https://stackoverflow.com/questions/12048576/how-to-find-eof-through-fscanf
https://stackoverflow.com/questions/21589353/cannot-convert-stdbasic-stringchar-to-const-char-for-argument-1-to-i
https://stackoverflow.com/questions/236129/the-most-elegant-way-to-iterate-the-words-of-a-string
https://stackoverflow.com/questions/191757/how-to-concatenate-a-stdstring-and-an-int
http://ysonggit.github.io/coding/2014/12/16/split-a-string-using-c.html
https://stackoverflow.com/questions/14265581/parse-split-a-string-in-c-using-string-delimiter-standard-c
https://stackoverflow.com/questions/8960087/how-to-convert-a-char-array-to-a-string
i fileOut=fopen((std::to_string(i)+".durations").c_str(),"w");
i是int
截取string的部分,这里用来换文件名后缀,具体见 http://www.cplusplus.com/reference/string/string/substr/
string filename=argv[i];
std::size_t pos=filename.find(".kwy");
string preName=filename.substr(0,pos);
fileOut[i-1]=fopen((preName+".durations").c_str(),"w");
用cin, cout似乎可以省区打开文件读写文件这些复杂的操作 ,然后在terminal 用cat,|, >>这些就解决了
出现的一个问题:
文本大概为这样的:
1 0 37 37 t
2 37 71 34 t
3 71 97 26 t
4 97 137 40 t
以下代码竟无法使fscanf读完一行再读下一行,只读了第一行,那些值就都不变了
while(fscanf(fileIn,"%d%d%d%d%c",&epiNum,&startTime,&endTime,&duration,&results)!=EOF)
{
cout<<epiNum<<" "<<duration<<endl;
}
这里可以解决这个问题:https://support.microsoft.com/en-us/help/60336/the-fscanf-function-does-not-read-consecutive-lines-as-expected
用其中的第一种方法更正后为:
while(fscanf(fileIn,"%d%d%d%d%c%[^\n]",&epiNum,&startTime,&endTime,&duration,&results)!=EOF)
{
cout<<epiNum<<" "<<duration<<endl;
}
另一个问题:
while(fscanf(fileIn[i-1],"%d%d%d%d%s",&epiNum,&startTime,&endTime,&duration,&results)!=EOF)
{
cout<<epiNum<<" "<<duration<<endl;
}
会出现错误,大概是这样 free(): invalid pointer:
这应该是因为最后一行没东西,string results接收不到东西,所以这样,所以改成:
while(fscanf(fileIn[i-1],"%d%d%d%d",&epiNum,&startTime,&endTime,&duration)!=EOF)
{
cout<<epiNum<<" "<<duration<<endl;
fscanf(fileIn[i-1],"%s",&results);
}
就ok了.
至于上面为什么把results 由前一个问题中的char改成string,是因为在文件中最后一个字符有t和o两种,当出现o时会出现一些问题,不知何解,导致得不到我想要的结果,然后发现改成string也能解决上一个问题中不能自动读下一行的问题,可能跟最后结尾的"\n"有关,可能o的时候后面没有"\n"??
- 关于读取文件 https://stackoverflow.com/questions/2799789/what-c-library-do-i-need-to-get-this-program-to-compile
#include <fcntl.h> //needed for O_RDONLY
#include <unistd.h> //needed for file open
#define RL_MEMORY_SIZE 1048576
int main(int argc,char * argv[] )
{
char* TargetWeightFile;
double* weightTarget=new double[RL_MEMORY_SIZE];
for(int i=0; i<RL_MEMORY_SIZE;i++)
weightTarget[i]=0;
int fileT=open(TargetWeightFile,O_RDONLY);
read(fileT,(char *) weightTarget, RL_MEMORY_SIZE*sizeof(double));
close(fileT);
return 1;
}
- 关于Makefile
TARGET = testWeights
CC = g++
all:: $(TARGET)
testWeights: testWeights.cpp
$(CC) testWeights.cpp -o testWeights
clean:
rm -f $(TARGET) *~
如果需要调试,可加-g,即
CC = g++ -g
上面其实只要一句就够了
g++ testWeights.cpp -o -g testWeights
gdb testWeights
就可以了.
Once gdb
starts, you can run the program using "r args".
So if you are running your code by:
$ executablefile arg1 arg2 arg3
Debug it on gdb
by:
$ gdb executablefile
(gdb) r arg1 arg2 arg3