[待总结整理]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<<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  
就可以了.
不过,如果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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值