Hi, my requirement is that I do gets and take user name as input. Now I have to create a file name with the same name as user name.
Next if I try to login as a same user from different client I need to check if that file with user. Txt already exists
If it exists then new file with same name must not be created

My problem is, how to compare user name with the name of the file
Program must be written in C

4 Years
Discussion Span
Last Post by Ancient Dragon

Hi vineeth.shankar1,

My problem is, how to compare user name with the name of the file

How are you doing it?
Please check the documentation and usage of strcmp like so man strcmp on nix systems.

Edited by 2teez


If you already know the name of the file to check, then there are several options. One option is try to open the file for reading, if that succeeds then you know the file already exists. Another way is to call _stat() (on Windows) or stat() (on *nix) which will return 0 if the file exists.


I don't need to know if file exits.
I have several files like
Input. Txt, output. Txt etc
When I do scanf (%s, output)
This output if matches with output. Txt, then I need to write inside output. Txt
Basically I need to compare variable name with the name of the file and not the contents


That's what I suggested -- once you get the filename in output in order to compare it with the name of an actual file all you do is find out if the file already exists. If it exists then you know there was a match. You could iterate through all the files in the folder and compare each filename with the text in output, but that would be the hard and time consuming way to do it.

Edited by Ancient Dragon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.