[待总结整理]C++中文件的读取和数据的处理

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<" "< 
  
        }
 
  

这里可以解决这个问题: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<" "< 
  
        }


另一个问题:

   while(fscanf(fileIn[i-1],"%d%d%d%d%s",&epiNum,&startTime,&endTime,&duration,&results)!=EOF)
        {
            cout< "<
        }
会出现错误,大概是这样 free(): invalid pointer: 

这应该是因为最后一行没东西,string results接收不到东西,所以这样,所以改成:

   while(fscanf(fileIn[i-1],"%d%d%d%d",&epiNum,&startTime,&endTime,&duration)!=EOF)
        {
            cout< "<
            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  //needed for O_RDONLY
#include  //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
    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  
就可以了.
不过,如果testWeights带参数,就先还是上面的方法打开gdb进入调试,然后在用r (就是run啊)运行的时候带上参数(https://stackoverflow.com/questions/14494957/how-do-i-pass-a-command-line-argument-while-starting-up-gdb-in-linux),不过我通常会在这之前先设好断点

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


你可能感兴趣的:(编程语言)