Data files...

Posted on 1998-12-11
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 70 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.

good luck

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

860 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