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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

777 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