-3

我需要将程序的这一部分从 C++ 转换为 C,你能帮我吗?

float ** OpenRotGrid_H(float **grid, unsigned int &nl, unsigned int & nc, bool &ok){
    ifstream fs;
    string line, num;
    unsigned int l(0),c(0),i(0),ncmax(0);

    fs.open("CarmY.txt");
    if (!fs) {
        //std::cout<<"Error: the Error file 'CarmY.txt' cannot be open\n\n";
        ok=false;
    }
    else{
        //size of the file
        getline(fs,line);
        ncmax= line.size()/MAXFLOAT;
        nl++;
        while(getline(fs,line)){
            nl++;
        }
        fs.clear();

        //Read CarmY values ( = grid)
        grid = initialize(nl,ncmax);
        fs.seekg(0,ios::beg); //initial position in the file

        while(getline(fs,line)){
            i=0;
            c=0;
            while(i<line.length()){
                while(i<line.length() && line[i]!='\t'){
                    num.push_back(line[i]);
                    i++;
                }
                grid[l][c]=(float) atof(num.c_str());
                c++;
                num.clear();
                if(i<line.length() && line[i]!='\n') i++;
            }
            l++;
        }
        nc=c;
        fs.close();
    }
    return grid;
}

int main(){

unsigned int nl=0 , nc=0 , nangle=0, nrot=0;
float **grid;
bool ok;

grid=initialize(nangle, nrot);
OpenRotGrid_H(grid, nl, nc, ok);


return 0;

} 

文件“C-arm”有一些不同长度的数字行,这个程序打开这个文件并取出每个数字并将其写入网格。

例如,我不知道我可以使用什么来代替getline(fs,line),fs.seekg和 ...?

4

1 回答 1

2

好吧,击中明显的候选人:

  • C中没有引用(例如unsigned int &nl),因此您需要传递指针并通过取消引用这些指针来更改基础数据。
  • 你可以只使用int而不是bool.
  • C,中没有ifstream对象,请使用fopen调用来打开文件。
  • getline可以变成fgets.
  • 您可以使用fseek而不是seekg.
  • C 中没有智能字符串,您需要在稍低的级别(字符数组)管理它们。
  • length可以替换为strlen

几乎可以肯定还有其他一些小问题,但这应该是一个好的开始。

于 2012-12-04T13:19:57.233 回答