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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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

Technology Partners: 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

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!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
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…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

801 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