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

Recommended Answers

All 4 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.