Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do i read a file into a variable?

Status
Not open for further replies.

iwuvc

Technical User
Jan 11, 2006
10
US
I have a text file called s1.txt, and it has one value of 12345. Can somebody post an example of how to read this file into a variable? My tries have been pointless, I can't get fopen to work for my purpose.
 
Code:
const size_t BUF_SIZE = 1024;
FILE* pFileIn = NULL;
char buf[ BUF_SIZE ];

if ( (pFileIn = fopen( "s1.txt", "rt" )) != NULL )
{
   if ( fgets( buf, BUF_SIZE, pFileIn ) != NULL )
   {
      /* Do something with your string. */
   }
   else
   {
      /* Error reading string. */
   }

   fclose( pFileIn );
}
else
{
   /* Error opening file. */
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top