Question : geting a word from an  external file into a variable in vc++ to be compared

Hello

I have a win32 application made in VC++.

1. I need to read the content of an external file ( a .txt for example)  and get its first line.
I need to read just a word and put it into a variable in my VC++ application.

2. Then I need to compare this varibale with my local string and if it matches, do some actions.


How can i do it?

Kind regards!

Answer : geting a word from an  external file into a variable in vc++ to be compared

I think you may want to create a console application for that. Just create a new project and select Win32 Console Application. You should then be able to paste you code in the main.cpp or any other main file.

This is a win32 application and therefore you wont be able to see the printf as there wont be any console. Here is how this one may work (though im not whether thats what would like it to be).

1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
#include <stdio.h>
#include <string.h>
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) 
{
   FILE * pFile;
   char mystring [100];

   pFile = fopen ("C:\\myfile.txt" , "r");
   if (pFile == NULL) 
      ::MessageBox(NULL, "Error opening file", "Application", MB_OK);
   else 
   {
     fgets (mystring , 100 , pFile);
     fclose (pFile);
     if(strstr(mystring, "sergio") == (char*) &mystring[0])
     {
          ::MessageBox(NULL, "matched, do action here!", "Application", MB_OK);
     }
   }
  return 0;
}
Random Solutions  
 
programming4us programming4us