basic encrypt

hi, i want to write a program who do a simple encrypt on a txt file below i put my questions. sorry for my bad english. if I forgot some library or did I put too much pots on me? Thanks for help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  #include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void c(string &x)
{
	char a;
	int i=0;
	do{
		a=x[i];
		a++;
		x[i]=a;
		i++;
    }while(a!='/');//how can i understand when to finish this while?
}


int main()
{
	int i=0, b;
	const int n=100000;//there is a const bigger which i can use?
	string a[n];
	string s;
	
	cout<<"0"<<endl;
	ifstream f("ps1.txt"); 
	while(f.good()) 
	{
		getline(f, s);
		a[i]=s;
		i++;
	}
	f.close();
	b=i;
	i=0;
	
	fstream f1("p1s.txt", ios::app);
	while(i<=b)
	{
		c(a[i]);
		s=a[i];
                         //how can I let the program automatically insert new 
                         //strings into the file?
		f1<<s<<endl;
		i++;
	}
	f1.close();
	system("pause");
	return 0;
Last edited on
You need the operation to happen on every element in the string.

1
2
3
4
5
6
7
8
void c(string &x)
{
  
  for ( auto& a : x)
  {
        a++; 
   }
}


This will require a compiler that understand C++11 or later.
I do not understand, can you explain whith an example?

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

void c(string &x)
{
  
  for ( auto& a : x)  // for each element in the string
  {
        a++;  // increment the element
   }
}

int main()
{
  string x;
  cin >> x;
  c(x);
  cout << '\n' << x;
}

ok, thanks is better than my idea, but how can I let the program automatically insert new
strings into the file?
<limits> has the largest constants for various integers. Or you can just know that an integer can hold 2 to the N-1 where n is the # of bits, so a 32 bit integer can hold 2 to the 32 -1 (unsigned).

not sure what exactly you are asking about inserting new strings.

playing around, casual home-use encryption, its hard to beat using xor on each byte of the file. But that tends to produce binary files, so the encrypted data can't be read with a text editor. Decrypted is unchanged of course, whatever the original was.


for autonomous way I meant I would want the subject to write a text file with information opens the program and the crypta program by itself the file.txt, the method I thought is the following: the file saves all the files strings inside a strings array of crypts and then rewrites them on the text file, and this must happen without any input from the subject.
Topic archived. No new replies allowed.