Go Premium for a chance to win a PS4. Enter to Win


Data files...

Posted on 1998-12-11
Medium Priority
Last Modified: 2010-04-02
I have an application that has three passwords. In this application I can change all of them if I want to. The three passwords are stored in an ordinary text file. When the program begins, the passwords are read from the file. When the program closes, the new passwords are saved to the file.

My question is this,

1. How do I read the passwords from the file?
I did use this code but it has problems when the password is set to nothing, ie no password
            char password1 [10];
            fscanf( pf, "%[^\n]\n", password1 );
            char password2[10];
            fscanf( pf, "%[^\n]\n", password2);
              char password3[10];
            fscanf( pf, "%[^\n]\n", password3);
I thought the scan would read until a new line so each password was on a new line.

2. Obviously if someone just opens the text file, then they have the passwords. How do I write to the file and change the characters that are written?

Thank you
Question by:rose337

Author Comment

ID: 1179922
By the way, the reason for the parameters in fscanf is that the passwords may be more than one word seperated by spaces. So my idea was that each password would be on seperate line in the text file and the scan would read to the end of the line (\n). This worked great until I change the password to nothing so the text file had an empty line and crash!

Expert Comment

ID: 1179923
There is an encryptor that you can use.  But I will if you don't want anything to technical then I suppose I will give this as an answer...

Accepted Solution

The_Brain earned 210 total points
ID: 1179924
There are two things that you can do anyway, just to optimise.

char password[3][10]={{'\0}}

/*init of array. this is saying 3 passwords with a length of 10 characters... password[0], password[1] and password[2] there are the three password, each has length potential of 10 char.*/

for (int count=0; count<3; count++)
   fscanf( pf, "%[^\n]\n", password[count]);

/*this is done three times so that the password[0]----> to password[2] can be accessed, in factnow with minimal fuss you can add any amount of passwords by just increasing the array password[x][10]*/
the scanf is a great thing to use, I haven't had to much to do with it.  but the empty line problem is obvious, but I can't solve that.  don't make no password.  make a default password

but now, to encrypt not too complicated, just so that you get the idea. //without the original comments.
I wouldn't use, scanf for such a small thing.
I would do this...

char password[3][10]={{'\0}}
char inputfile[]="password.txt"

ifstream input(InputFile);

if (!input)
  cout<<"ERROR in file name.";
   for (int count=0; count<3; count++)
        input.getline(password[count],10); /*puts the line in password[count] */
     /* for the encrypt;
     for (int i=0; i<10; i++)
           password[count][i]+=12;        /*shifts all the letter by 12 positions.*/
//This is only encrypt reading, I have n't actually put this encrypted form into the file, as you can see, I only read and encrypted,
I don't wanna do it for you so do whatever you  do to get and put things into the file, and use a similar kind of algo, USE 3d arrays for this kind of thing.  that is what makes it easier If you really want me to I will do this for you,  you will have to e-mail me for it.  g_dutoit@hotmail.com

good luck

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 22

Expert Comment

ID: 1179925
A shift substitution cypher like that (where a value is added to each digit) is extremely easy to crack.  It ca ussually be cracked on the first try, but if not a few attempts will do it.  (256 attempts are guaranteed to do it.)

A rolling XOR would be better for this,  Again it is not high security, but since the data length is short it is hard to crack the XOR.  In fact,if you make XOR pattern 30 or more in length, it will never repeat which makes this harder to crack.  However, if the user has the ability to change their own passwords, then they can learn the XOR pattern (or the shift in the shift cypher above),  and can crack others passwords easily (1st attempt).

Expert Comment

ID: 1179926
The_Brain>> char password[3][10]={{'\0}} ??

//Not sure?
Is the 11 character the \0? and the 12 character the adress of the next X in [X][10]?

Your sincerely

Expert Comment

ID: 1179927

password[3][10] three passwords with 10 characters each.
10th char is '\0'

"012345678\0" is the limit.  Well sorry to answer the query soo late. I just never saw the answer.  (THE BLOODY EE won't e-mail me to tell me which Questions are changed!)

:^) well happy new year!

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question