473,659 Members | 3,592 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read, Modify, and Write Data from and to a file

Mighty Mackinac
4 New Member
Hi all,

I am trying to write a program that can encrypt and decrypt files using an extremely basic encryption scheme. Here's a list of what I still need:
1. Read scheme from file to be applied to input file
2. Modify input to match the scheme
3. Write output to new file

My first actual question is what is the code to read two different sets of data from one file e.i. A (3,1) on the first line, next line is B (4,6), etc.

I need to take any character from the file and assign it a coordinate to be output to a new file, and vise-versa.

Just to clarify: First set of data is the actual character, the second set being its equivalent coordinate.

here's what i got so far:

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6.  
  7. using namspace std;
  8.  
  9. int main()
  10. {
  11.     char begin;
  12.     ifstream datafile, fin;
  13.     ofstream fout;
  14.     string filename;
  15.  
  16.     datafile.open("sequence.dat");
  17.  
  18.     start:
  19.  
  20.     cout << "(D)ecrypt or (E)ncrypt ==> ";
  21.     cin >> begin;
  22.     if (begin == 'E' || begin == 'e')
  23.     {
  24.         cout << "Please enter the input file name: ";
  25.         cin >> filename;
  26.         fin.open( filename.c_str() );
  27.  
  28.  
  29.  
  30.  
  31.  
  32.         fout.open("encrypted.txt");
  33.         fout <<
  34.     }
  35.     else if(begin == 'D' || begin == 'd')
  36.     {
  37.         cout << "Please enter the input file name: ";
  38.         cin >> filename;
  39.         fin.open( filename.c_str() );
  40.  
  41.  
  42.  
  43.  
  44.         fout.open("decrypted.txt");
  45.         fout << 
  46.     }
  47.  
  48.     return 0;
  49. }
  50.  
Oct 16 '08 #1
4 4173
Mighty Mackinac
4 New Member
here's an update after some tinkering.

Expand|Select|Wrap|Line Numbers
  1. fin.open( filename.c_str() );
doesn't seem to work. i can't verify or disprove that it is working or not.

I can create new files, i have tested that, but i don't know if i can read from files.

here is new code,

Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <string>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11.     char begin;
  12.     ifstream datafile, fin;
  13.     ofstream fout;
  14.     string filename;
  15.  
  16.     datafile.open("scheme.dat");
  17.  
  18.     cout << "(D)ecrypt or (E)ncrypt ==> ";
  19.     cin >> begin;
  20.     if (begin == 'E' || begin == 'e')
  21.     {
  22.         cout << "Please enter the input file name: ";
  23.         cin >> filename;
  24.         fin.open( filename.c_str() );
  25.  
  26.         if(!fin)  //check for file validity
  27.         {
  28.             cout << "Can't open the input file" << endl;
  29.         }
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.         fout.open("encrypted.txt");
  37.         //fout << 
  38.     }
  39.     else if(begin == 'D' || begin == 'd')
  40.     {
  41.         cout << "Please enter the input file name: ";
  42.         cin >> filename;
  43.         fin.open( filename.c_str() );
  44.  
  45.         if(!fin)   //check for file validity
  46.         {
  47.             cout << "Can't open the input file" << endl;
  48.         }
  49.  
  50.  
  51.  
  52.  
  53.  
  54.         fout.open("decrypted.txt");
  55.         //fout << 
  56.     }
  57.  
  58.     return 0;
  59. }
  60.  
Oct 16 '08 #2
oler1s
671 Recognized Expert Contributor
Read in a line. Process that line. Repeat.
Oct 16 '08 #3
Mighty Mackinac
4 New Member
ok...got it to read line by line...but i need to figure out how to take my scheme and apply it to the text. I was thinking i could use a matrix, but i have never done matrices in C++. I found a way to replace parts of a string with words or characters, could be useful.

Find and Replace C++
Oct 16 '08 #4
Mighty Mackinac
4 New Member
what do you think about using maps? i saw a program that used them, thought that it would be easier
Oct 16 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
12653
by: Graham Taylor | last post by:
I've tried posting this in the 'microsoft.public.access' but I will post it here also, as I think it might be the webserver which is causing my problem. --------- I have an Access 2003 database which is in the "fpdb" folder of my webserver. Its located there so that I can use asp to build a web-based front-end for users to read the database - http://www.nist.ac.th/maths/test1.asp The MBD file is edited using Access (2003) and opening...
1
1594
by: ezmeralda | last post by:
Hello, I have to solve the following task: - create an editor-application in c# to read, modify and create xml-files - .xsd-schema-file is available and can be used Currently, I am thinking of the following way to do the job: - use xsd.exe to create basic c#-classes from xsd.file (modify these classes according to my particular needs)
1
4297
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
4
20564
by: Carlos AZ | last post by:
I have the following XML file named (applicattion.exe.config): <?xml version="1.0" encoding="utf-8" ?> <configuration> <AppSettings> <add key = "PathBD" value = "c:\project\data\maco.mdb" /> </AppSettings> </configuration>
3
35000
by: Testguy | last post by:
Hi, I have been reading various messages in this group, and could not find an answer regarding what I am trying to do. At least, I could not find an easy answer. I am trying to figure out how to read and XML file and put the data in fields in a VB application, and also how to write the information to an XML file (either to a new file or modifying an existing file.
2
3685
by: Tiger | last post by:
I try to write a struct into a brut file but I can't write all var in this struct when I try to add user I have that : > testmachine:/usr/share/my_passwd# ./my_passwd -a users.db > Ajout d'une entrée dans la base : > > Username : test > Password : aze > Gecos : qsd
2
1404
by: =?Utf-8?B?YW5rMmdv?= | last post by:
Thanks in advance for reading this. Let's say I have a file (file01) with this data in ASCII (ignore line col): line01 123abc line02 Header01 Starts blah var line03 detail01 000001 line04 detail02 000002 line05 detail03 000003 line06 detail04 000004
2
3375
by: peter.vanna | last post by:
I would like to write a program to read a text file (data.txt) on Windows and Linux machines. data.txt is formated as follows: 1 10 100 1000 2 20 (a new but empty line)
1
1054
by: Jim | last post by:
I'm a newbie to vb .net. I've got a couple of test programs to read and write text files and one to read and display Access data. I need to be able to read the text file, massage the data and write it to a table in an Access database. Any suggestions as to where I can find an example of that? I just need an example to copy and modify to learn from. Thanks in advance.
0
8337
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8748
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7359
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1978
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1739
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.