Click here to Skip to main content
15,910,872 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Can you please tell me that how to check the end of file concept in vc++ while reading the records from the file. Here I attached my script... it's compiled successfully. But a run time error is occurred while executing this program...

What I have tried:

C++
// KillUser.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "malloc.h"
#include <string.h>
#include <process.h>
#include <stdio.h>

int main()
{
	FILE *p;
	char user[20];
	char cmd1[100], cmd2[100];
	//int errorCode;

	/*clrscr();*/

		strcpy_s(cmd1, "TASKKILL /F /FI \"USERNAME eq ");
		strcpy_s(cmd2, " /IM ntbshell.exe");

		/*char cmd1[] = "TASKKILL /F /FI \"USERNAME eq ";
		char cmd2[] = " /IM ntbshell.exe";*/

		/*printf("%s",cmd2);
		getch();*/
		
		errno_t errorCode = fopen_s(&p,"c:\\ERPLN\\TaskControl\\InforControlUser.txt", "r");
		
		while (!feof(p))
		{
			fscanf_s(p, "%s", &user);
			//printf("%s",user);

			strcat_s(user, "\"");

			strcat_s(cmd1, user);
			strcat_s(cmd1, cmd2);
			printf("%s\n", cmd1);
			//system(cmd1);
			//getch();

			strcpy_s(cmd1, "TASKKILL /F /FI \"USERNAME eq ");
			strcpy_s(cmd2, " /IM ntbshell.exe");

			/*char cmd1[] = "TASKKILL /F /FI \"USERNAME eq ";
			char cmd2[] = " /IM ntbshell.exe";*/
		}
		fclose(p);
		/*system("TASKKILL /F /IM notepad.exe");*/
		return 0;
	}
Posted
Updated 25-Oct-17 1:38am
v2
Comments
Richard MacCutchan 25-Oct-17 7:35am    
What run time error, and where does it occur? Also, why do you not check the return value from fopen_s?

1 solution

C++
fscanf_s(p, "%s", &user);

You should not use the addressof operator (&) on the array name user. It should just be:
C++
fscanf_s(p, "%s", user); // no leading &
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900