Question:
C Programming. How to call a .txt file?
James
2012-11-29 19:49:07 UTC
I had a question about getting C to read information from a .txt file. I went to source file -> add existing file -> and inserted the .txt file but its unsuccessful. Here is a simple code I am using. Please help me understand whats wrong. Thanks you :)


#include
int main()
{
FILE* f = fopen("CTestMaze.txt", "r");
fclose(f);
}

I can't get it any simpler than that. The Error that occurs is... :
Error 1 error C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

When I use fopen_s instead, like it suggest, I then get the error... :
Error 1 error C2660: 'fopen_s' : function does not take 2 arguments


Can you help me understand why I am getting this error and how to fix it?
Four answers:
anonymous
2012-11-29 19:55:27 UTC
excellent question!

there are several ways to read information from the .txt file

please refer the following codings

/*

** File FILE_3.C

**

** Illustrates how to read from a file.

**

** The file is opened for reading. Each line is successively fetched

** using fgets command. The string is then converted to a long integer.

**

** Note that fgets returns NULL when there are no more lines in the file.

**

** In this example file ELAPSED.DTA consists of various elapsed times in

** seconds. This may have been the result of logging the time of events

** using an elapsed time counter which increments each second from the

** time the data logger was placed in service.

**

** Typical data in elapsed.dta might be;

**

** 65

** 142

** 1045

** 60493

** 124567

**

**

** Peter H. Anderson, 4 April, '97

*/



#include /* required for file operations */

#include /* for clrscr */

#include /* for delay */



FILE *fr; /* declare the file pointer */



main()



{

int n;

long elapsed_seconds;

char line[80];

clrscr();



fr = fopen ("elapsed.dta", "rt"); /* open the file for reading */

/* elapsed.dta is the name of the file */

/* "rt" means open the file for reading text */



while(fgets(line, 80, fr) != NULL)

{

/* get a line, up to 80 chars from fr. done if NULL */

sscanf (line, "%ld", &elapsed_seconds);

/* convert the string to a long int */

printf ("%ld\n", elapsed_seconds);

}

fclose(fr); /* close the file prior to exiting the routine */

} /*of main*/





thank you!
Cubbi
2012-11-29 20:09:46 UTC
Indeed, fopen_s does not take 2 arguments. You can read about its arguments at MSDN http://msdn.microsoft.com/en-us/library/z5hh6ee9(v=vs.110).aspx or in a modern C language reference (that function became part of C last year).



Note that there is nothing at all wrong with fopen(), it is safe to use _CRT_SECURE_NO_WARNINGS. It may even be advantageous, since not many compilers support fopen_s(), your program will be more portable with fopen()
?
2012-11-29 20:36:42 UTC
fopen_s is microsofts attempt to replace fopen. You can either switch to use fopen_s, find a way to disable this microsoft feature, or use another compiler like gcc.



Similar to strcpy and strcpy_s.
Locoluis
2012-11-29 20:39:38 UTC
Don't use Microsoft's "_s" functions. They're unportable. Your code will only compile in Microsoft Visual C++, and you'll be forever tied to the chains of Redmond.



Instead, obey the C89 standard, and disable the deprecation warnings.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...