0

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.

Simple representation of my code:

#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;   
}

Also to note, this is for c++98.

Melebius
  • 4,692
  • 3
  • 32
  • 43
jabroni
  • 97
  • 8
  • Since you're using C++, why `fscanf`? Also C++98!? Why that constraint? – tadman May 18 '20 at 07:25
  • 3
    `scanf` and family are inherited from C. They know *nothing* about C++ objects like `std::string`. Just about any book or tutorial should have shown you how to read "words" from the standard input stream `cin`, reading from a C++ file stream (`std::ifstream`) works just the same. A decent book should have information about files and file-streams as well. [Here's a list of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). Please invest in a couple. – Some programmer dude May 18 '20 at 07:25
  • @tadman legacy code :( – jabroni May 18 '20 at 07:30
  • Newer compilers should be able to deal with legacy code just fine. C++ in general doesn't have a lot of breaking changes. – tadman May 18 '20 at 07:31
  • You may want to look at [reading file into `std::string`](https://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring) as a place to start. That's a ten year old answer, though only half as old as your compiler. – tadman May 18 '20 at 07:33
  • Why do you use `fscanf(3)` or any `` functions in C++? Should Stroustrup hear you, just prepare for the blame! – Luis Colorado May 18 '20 at 18:02
  • Have you checked the return value of `fopen(3)` ???? if you pass `NULL` to `fopen(3)` one of the things you can receive from it is `-1`. – Luis Colorado May 18 '20 at 18:05
  • Is `fscanf(fTime, "%s\n", &startTime)` in your actual code? Because the `%s` format specifier needs a `char*` parameter, not a `std::string*`. A decent compiler would give you a warning about that. – David Schwartz May 18 '20 at 18:27

1 Answers1

0

You probably are receiving NULL as result from fopen(3) (does the file actually exist? Have you permissions to write on that file? in the directory?)

Change your code into:

#include <iostream>

using namespace std;

string startTime;

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

    if (!fTime) {
        perror("fopen");
    }

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

    fclose(fTime);
    return 0;   
}

And you probably will have an idea of what's going on. You probably don't have the proper access rights to create a file called time_temp.txt in your current directory, the fopen(3) returns NULL, and you end calling fscanf(NULL, "%s\n", ....

Luis Colorado
  • 8,037
  • 1
  • 10
  • 27