用空格读取文本(c ++)

时间:2012-02-16 09:01:14

标签: c++ text-files

我正在尝试读取.txt文件,在我的文本文件中有数字但也有路径文件夹(例如:“C:\ Documents and Settings ......”)

我能够在没有任何问题的情况下读取数字,但是当我尝试读取路径时,我只是阅读直到第一个“空格”,这意味着,我只读了两个案例“C:\ Documents”。我该怎么办?

//READING PARAMETERS FROM A .TXT FILE
//double sigma; double alpha; double tau;
//double miniter; double maxiter; double q; double m;
//‘R’ image folder ; ‘T’ image folder; 

// input-file-stream
//  - in = (input) open the file for reading
ifstream f ( "parametros.txt", ifstream::in ) ; 

// Error if it is not open properly
if ( !f){
    cout<<"ERROR OPENING THE FILE .TXT"<<endl;
    exit(EXIT_FAILURE);
}


char *cadena = new char [100];
stringstream  folder_T; 
stringstream  folder_R;
int iter = 1;
while ( !f.eof() ){
    // reading the next line
    f >> cadena ;
    if ( iter == 1 ){
        sigma = atof(cadena);
    }
    else if ( iter == 2){
        alpha = atof(cadena);
    }
    else if ( iter == 3){
        tau = atof(cadena);
    }
    else if ( iter == 4){
        miniter = atof(cadena);
    }
    else if ( iter == 5){
        maxiter = atof(cadena);
    }
    else if ( iter == 6){
        q = atof(cadena);
    }
    else if ( iter == 7){
        m = atof(cadena);
    }       
    else if ( iter == 8){
        folder_T<<gets(cadena);      // Only “C:/Documents”  is returned
    }
    else if ( iter == 9){
        folder_R<<gets(cadena);      // Only “C:/Documents” is returned
    }

    iter = iter + 1;

}// fin while
f.close();

1 个答案:

答案 0 :(得分:3)

您没有在cadena中阅读完整的一行。这样做

getline(f, cadena);

另外,你不应该使用gets()。您已经在cadena中拥有完整的一行。