Parsing Text File Contents

Hello,

I have the below script that I am trying to use to take each line of a text file to read the numerical value of each flag.state. Everything compiles but I do not get a message box confirming whether any of the flags are checked or installed even though that if this script was working properly it should give me. Is there something I am doing wrong with the below script? I do have other code that opens the file in question and I do get confirmation that it has done so, that does not appear to be the problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
	std::string line1;
			std::getline(ifs, line1);
			
			std::stringstream line1stream(line1);
			int rsa;
			line1stream >> rsa;
				if (rsa == 1){
				  RSAFlag.State = Checked;
				  MessageBox(hwnd, "RSA Checked", NULL, MB_OK);
				}else if(rsa == 2){
					   RSAFlag.State = Installed;
					  }
			
			std::string line2;
			std::getline(ifs, line2);
			
			std::stringstream line2stream(line2);
			int chrome;
			line2stream >> chrome;
				if (chrome == 1){
					CHROMEFlag.State = Checked;
					MessageBox(hwnd, "Chrome Checked", NULL, MB_OK);
				}else if(chrome == 2){
					   CHROMEFlag.State = Installed;
				}
			
			std::string line3;
			std::getline(ifs, line3);
			
			std::stringstream line3stream(line3);
			int skype;
			line3stream >> skype;
				if (skype == 1){
					SKYPEFlag.State = Checked;
					MessageBox(hwnd, "Skype Checked", NULL, MB_OK);
				}else if(skype == 2){
			           SKYPEFlag.State = Installed;	
				}		
I do not get a message box confirming whether any of the flags are checked or installed

This may not be the problem, but the code above only shows a messagebox for the "Checked" condition. There's no indication for "Installed". (and no action for the case where it is neither of these).
Yeah I am only doing the checked value for right now, even when there is a 1 in the text file it does not give me the message box. Is there something wrong with the way that it's reading the file? In the actual text document this is how it would show up:
1
2
3
4
5
1

1

1
I've not tried to run the code, but it looks reasonable (unless I miss the obvious, which is always possible).

Your text file appears to have intervening blank lines (lines 2 and 4) which may affect things. Have you tried using a debugger to track the variables during execution. (or failing that, display or output to a file the values of line1 and rsa to check their contents).
I haven't tried a debugger because I am not sure how that all works lol. Yeah I have endl; but did remove that at one point blank lines and it still didn't want to work. this is what I have to open the file which is right above the last chunk of code:

1
2
3
4
5
6
7
8
9
10
11
	std::ifstream ifs;
	std::wstring str;
	std::wstring str2 = L"-finish";
	str = GetCommandLineW();
	size_t found;
	found = str.find(str2);
    MessageBoxW(NULL, GetCommandLineW(), NULL, MB_OK);
    if (found != std::wstring::npos){
    	ifs.open("E:\\StateFile.txt");
		MessageBox(hwnd, "StateFile Opened", NULL, MB_OK);	  	        				    			
			}
There is no actual check for the file opening properly.
1
2
3
4
5
6
    std::ifstream ifs("E:\\StateFile.txt");
    
    if (ifs.is_open()) 
        MessageBoxA(hwnd, "StateFile Opened", "Information", MB_OK);
    else
        MessageBoxA(hwnd, "StateFile could not be opened", "Error", MB_ICONERROR);
Last edited on
Hmm says it couldn't be opened
Is that reasonable - are the path and filename correct?
Yeah they are the same, I am horribly confused now lol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
	std::ifstream ifs;
	std::wstring str;
	std::wstring str2 = L"-finish";
	str = GetCommandLineW();
	size_t found;
	found = str.find(str2);
    MessageBoxW(NULL, GetCommandLineW(), NULL, MB_OK);
    if (found != std::wstring::npos){  
    	 ifs.open("E:\\StateFile.txt"); 	        				    			
			}  
			
	if (ifs.is_open()){
		MessageBoxA(hwnd, "StateFile Opened", "Information", MB_OK);  
    		}else{
				MessageBoxA(hwnd, "StateFile could not be opened", "Error", MB_ICONERROR);
				 }
Then presumably the file is not open because the if statement at line 8 is false:
 
    if (found != std::wstring::npos){

I did have a messagebox in that if statement though to verify that the statement was true, so it was finding the -finish.
I see only this:
1
2
3
4
    if (found != std::wstring::npos)
    {
        ifs.open("E:\\StateFile.txt"); 	        				    			
    }  

there's just a single statement between the opening and closing braces of the block controlled by that if statement.

Really, if that's the only place where the file is guaranteed to be opened, the rest of the code to handle the file should be part of that block too.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
    if (found != std::wstring::npos)
    {
        ifs.open("E:\\StateFile.txt"); 
        if (ifs.is_open())
        { 
            MessageBoxA(hwnd, "StateFile Opened", "Information", MB_OK);

            std::string line1;
            std::getline(ifs, line1);
            std::stringstream line1stream(line1);
            int rsa;
            line1stream >> rsa;
            if (rsa == 1)
            {
                RSAFlag.State = Checked;
                MessageBox(hwnd, "RSA Checked", NULL, MB_OK);
            }
            else if(rsa == 2)
            {
                RSAFlag.State = Installed;
            }

            // and the rest of the file accesses here

        }
        else
            MessageBoxA(hwnd, "StateFile could not be opened", "Error", MB_ICONERROR);
    }

Ok I changed that all up, as for why it's not opening the statefile do you think it's because of the way it's written too?

1
2
3
4
5
6
7
8
9
10
std::ofstream fout("E:\\StateFile.txt", std::ios_base::trunc);
								fout << RSAFlag.State << "\n";
							//	fout << std::endl;
																						
								fout << CHROMEFlag.State << "\n";
							//	fout << std::endl;
															
								fout << SKYPEFlag.State << "\n";
							//	fout << std::endl;
								fout.close();	
There are two separate stages. One is to open the file. The next is to read from it. Even if the file is completely empty, so long as the path and name are correct, and the file is not currently in use by some other process, then it should be possible to open it.

Since the code above closes the file, fout.close();, it should not be flagged as "in use" so there should be no problem with opening it.
I hesitate to suggest this, as I might not like the answer. Maybe you could post the complete code, rather than just fragments of it. Or if it's very long, perhaps create a separate, trimmed-down version which is still complete and able to demonstrate the problem.
Ok here is a breakdown:

In winMain:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
	std::ifstream ifs;
	std::wstring str;
	std::wstring str2 = L"-finish";
	str = GetCommandLineW();
	size_t found;
	found = str.find(str2);
    MessageBoxW(NULL, GetCommandLineW(), NULL, MB_OK);
    if (found != std::wstring::npos)
	{ 
		ifs.open("E:\\StateFile.txt", std::ios_base::trunc); 	
		if (ifs.is_open())
		{
			MessageBoxA(hwnd, "StateFile Opened", "Information", MB_OK); 
			
			std::string line1;
			std::getline(ifs, line1);
			
			std::stringstream line1stream(line1);
			line1stream.str("\n");
			int rsa;
			line1stream >> rsa;
				if (rsa == 1){
				  RSAFlag.State = Checked;
				  MessageBox(hwnd, "RSA Checked", NULL, MB_OK);
				}else if(rsa == 2){
					   RSAFlag.State = Installed;
					  }
			
			std::string line2;
			std::getline(ifs, line2);
			
			std::stringstream line2stream(line2);
			line2stream.str("\n");
			int chrome;
			line2stream >> chrome;
				if (chrome == 1){
					CHROMEFlag.State = Checked;
					MessageBox(hwnd, "Chrome Checked", NULL, MB_OK);
				}else if(chrome == 2){
					   CHROMEFlag.State = Installed;
				}
			
			std::string line3;
			std::getline(ifs, line3);
			
			std::stringstream line3stream(line3);
			line1stream.str("\n");
			int skype;
			line3stream >> skype;
				if (skype == 1){
					SKYPEFlag.State = Checked;
					MessageBox(hwnd, "Skype Checked", NULL, MB_OK);
				}else if(skype == 2){
			           SKYPEFlag.State = Installed;	
				}		
    	}
        else
        	MessageBoxA(hwnd, "StateFile could not be opened", "Error", MB_ICONERROR);
    }
		      

Example of Checkbox code:
1
2
3
4
5
6
7
8
9
10
11
 case WM_CREATE:                                 
             
             {                              																					
						/*  Create SecureID Checkbox  */
                        CreateWindowW(L"button", L"SecureID",
						WS_VISIBLE | WS_CHILD | BS_AUTOCHECKBOX,
						20, 240, 188, 35,
						hwnd, (HMENU) ID_RSA, NULL, NULL);
						
						
                      


1
2
3
4
5
6
7
8
9
10
   switch (LOWORD(wParam))
              {      								 						 					 																 
					 						  					 
					 /*  Identify If SecureID Is Checked  */
                     case ID_RSA:
                    	   
						if(IsDlgButtonChecked(hwnd, ID_RSA) == BST_CHECKED){                        
								RSAFlag.State = Checked;															    				           									  
											  }	
					 break;	


createprocess example:

1
2
3
4
5
6
7
8
9
10
11
12
13
 case ID_INSTALL:
                         
						 ShowWindow(hwnd, SW_MINIMIZE);                                                                             	
                           	      	                          	      
								  /*  Install RSA Secure ID  */															  										 								                        	  
                           	   	  if(RSAFlag.State == Checked){
								  CreateProcess(NULL,"C:\\Windows\\System32\\msiexec /i E:\\programs\\RSA\\RSASecurIDToken400.msi /passive",
												 NULL,NULL,FALSE,
												 CREATE_NEW_CONSOLE,
												 NULL,NULL,&si,&pi);						  						                                                                                     								  						         
						  WaitForSingleObject( pi.hProcess, INFINITE );
						           RSAFlag.State = Installed;						 						  						  							     	     
								           }

Writing to file in the end of the program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
		case WM_DESTROY:
            PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
            break;		
		default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);        
    }                              
      std::ofstream fout("E:\\StateFile.txt", std::ios_base::trunc);
								fout << RSAFlag.State << "\n";
							//	fout << std::endl;
																						
								fout << CHROMEFlag.State << "\n";
							//	fout << std::endl;
															
								fout << SKYPEFlag.State << "\n";
							//	fout << std::endl;
								fout.close();		
    return 0;

Almost forgot the createfile, this is in the very top:

 
HANDLE StateFile = CreateFile("E:\\StateFile.txt", GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, SECURITY_ANONYMOUS); 


The background of this program is that at one point it restarts, uses runonce to start the program again, checks for the -finish from the runonce command line, reads the statefile, and continues where it left off. Everything is good with the rebooting and checking for the -finish but the problem is the opening the statefile.
Topic archived. No new replies allowed.