fscanf尝试返回文本文件的第一行时返回-1

I am trying to simply return the first value from a text file I have called time_temp.txt. The contents of this file are:

1588567745.203
1588567745.203
1588567745.204
1588567745.204
1588567745.204
1588567745.205
1588567745.205
1588567745.205
1588567745.206
1588567745.206

All I am trying to do is return the value of the first line - so in this case: 1588567745.203.

When I do fscanf, it returns a value of -1, and some symbols inside my console. I have been trying to experiment with different operator types in the fscanf function, but that doesn't seem to return the value I am expecting.

我的代码的简单表示:

#include <iostream>

using namespace std;

string startTime;

int main()
{
    FILE *fTime =  fopen("time_temp.txt", "w+");

    cout << fscanf(fTime, "%s\n", &startTime) << endl;
    printf("nfirst = %s\n",startTime);

    fclose(fTime);
    return 0;   
}

还要注意,这是针对c ++ 98的。

评论