• 0

[C] File input and strcmp.


Question

Hello,

I am reading from a file of this format :

ID 1

name

name

name

etc.

ID 2

name

name

name

etc.

I am using fscanf to read (like this fscanf(file,"%s\n",data)), as far as I am aware fscanf will skip special characters right? So how can I understand when I get ID 1? I think it doesn't take the blank space....

Is there any other way besides reading char by char?

Thanks a lot

Link to comment
https://www.neowin.net/forum/topic/778574-c-file-input-and-strcmp/
Share on other sites

3 answers to this question

Recommended Posts

  • 0

Yes you're right. fscanf() will read whitespace and ignore it. You could maybe use something like fgets() as it will read the whole line in up to a \n

Then you can just use strcmp() to find out what the line is. Ensure you create a buffer of adequate size, however given the file you're reading in, it won't have to be that large at all.

  • 0
  ViZioN said:
Yes you're right. fscanf() will read whitespace and ignore it. You could maybe use something like fgets() as it will read the whole line in up to a \n

Then you can just use strcmp() to find out what the line is.

Cool thanks, I will try it out and report back. :)

EDIT : Ok, but if I use fgets and I reach the EOF, it will store EOF in my string variable??

How can I compare my string to an EOF?

Part of my code is like this : if (fgets(data,255,fin) != EOF

The above probably won't work though:P - would it work if I cast fgets?

Edited by gianpan
  • 0

It won't store the EOF within the string, but it will however store the \n within the string. If the EOF is encountered, fgets() returns a NULL pointer so I just checked for that.

Something like this may work:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main()
{
	FILE *pFile = NULL;
	char string[100];

	pFile = fopen("text.txt", "r+");

	if(!pFile){
		printf("File cannot be opened.\n");
		return 0;
	}

	while( fgets(string, 99, pFile) != NULL){
		if(strcmp(string, "ID 1\n") == 0){
			printf("ID 1 found\n");
		}else if(strcmp(string, "ID 2\n") == 0){
			printf("ID 2 found\n");
		}
	}

	printf("\nEOF reached\n");
	getchar();

	return 0;
}

There is more information here - http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

I tested it using the file

ID 1
ID 2
ID 1

Note there is a blank new line at the end of the file. This is to cope with fgets reading the \n.

You can just then modify so that once you've read an ID you can read the names. You may also want to read the file more than once so you can figure how much memory you will need to allocate to store all the names. This allows you to cope with different sizes of text file.

Hope this helps.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Now I may not quite understand this, so someone tell me if I'm off the mark here, but does this mean they'll be potentially removing drivers for now unsupported systems, such as old processors and chipsets? In the past 15 years, Windows has been amazing at just installing on any device, and often having zero, or just a few unessential drivers missing on first install. It would be a shame for that experience to go, though I understand the reasoning, or at least their financial reasoning for it!
    • Microsoft is removing legacy drivers from Windows Update by Usama Jawad Last month, we learned that Microsoft is making major changes to the development of hardware drivers in Windows. This included the retirement of Windows Metadata and Internet Services (WMIS), along with the process for pre-production driver signing. Now, the Redmond tech firm has informed partners that it will be getting rid of old drivers in Windows Update. In what is being described as a "strategic" move to improve the security posture and compatibility of Windows, Microsoft has announced that it will be performing a cleanup of legacy drivers that are still being delivered through Windows Update. Right now, the first phase only targets drivers that already have modern replacements present in Windows Update. As a part of its cleanup process, Microsoft will expire legacy drivers so that it is not offered to any system. This expiration involves removing audience segments in the Hardware Development Center. Partners can still republish a driver that was deemed as legacy by Microsoft, but the firm may require a justification. Once the Redmond tech giant completes its first phase of this cleanup, it will give partners a six-month grace period to share any concerns. However, if no concerns are brought forward, the drivers will be permanently eradicated from Windows Update. Microsoft has emphasized that this will be a regular activity moving forward and while the current phase only targets legacy drivers with newer replacements, the next phases may expand the scope of this cleanup and remove other drivers too. That said, each time the company takes a step in this direction, it will inform partners so that there is transparency between both parties. Microsoft believes that this move will help improve the security posture of Windows and ensure that an optimized set of drivers is offered to end-users. The firm has asked partners to review their drivers in Hardware Program so that there are no unexpected surprises during this cleanup process.
    • No idea, but I had a client the other week that lost the entire drive to it. I suggested relying on the Samsung T7's instead. The Sandisk Extreme's had reliability issues too.
    • I use it every day so personally yes I need it, or rather I want it. I use OpenShell though, not the garbage modern Start Menu. I just counted and at the moment I have a total of 92 program shortcuts organized into six folders almost exactly the way I did back in Windows 95. I can get to any program I want to run very quickly. I never use Search to find or run programs.
    • I do miss the Apps view from Windows 8.1 Update.
  • Recent Achievements

    • One Month Later
      KynanSEIT earned a badge
      One Month Later
    • One Month Later
      gowtham07 earned a badge
      One Month Later
    • Collaborator
      lethalman went up a rank
      Collaborator
    • Week One Done
      Wayne Robinson earned a badge
      Week One Done
    • One Month Later
      Karan Khanna earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      682
    2. 2
      ATLien_0
      274
    3. 3
      Michael Scrip
      220
    4. 4
      +FloatingFatMan
      171
    5. 5
      Steven P.
      160
  • Tell a friend

    Love Neowin? Tell a friend!