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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mighty Mackinac
    New Member
    • Oct 2008
    • 4

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

    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:

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namspace std;
    
    int main()
    {
    	char begin;
    	ifstream datafile, fin;
    	ofstream fout;
    	string filename;
    
    	datafile.open("sequence.dat");
    
    	start:
        
    	cout << "(D)ecrypt or (E)ncrypt ==> ";
    	cin >> begin;
    	if (begin == 'E' || begin == 'e')
    	{
    		cout << "Please enter the input file name: ";
    		cin >> filename;
    		fin.open( filename.c_str() );
    
    
    
    
    		
    		fout.open("encrypted.txt");
    		fout <<
    	}
    	else if(begin == 'D' || begin == 'd')
    	{
    		cout << "Please enter the input file name: ";
    		cin >> filename;
    		fin.open( filename.c_str() );
    
    
    
    
    		fout.open("decrypted.txt");
    		fout << 
    	}
    
    	return 0;
    }
  • Mighty Mackinac
    New Member
    • Oct 2008
    • 4

    #2
    here's an update after some tinkering.

    Code:
    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,

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    int main()
    {
    	char begin;
    	ifstream datafile, fin;
    	ofstream fout;
    	string filename;
    
    	datafile.open("scheme.dat");
        
    	cout << "(D)ecrypt or (E)ncrypt ==> ";
    	cin >> begin;
    	if (begin == 'E' || begin == 'e')
    	{
    		cout << "Please enter the input file name: ";
    		cin >> filename;
    		fin.open( filename.c_str() );
    
    		if(!fin)  //check for file validity
    		{
    			cout << "Can't open the input file" << endl;
    		}
    
    
    		
    
    
    		
    		fout.open("encrypted.txt");
    		//fout << 
    	}
    	else if(begin == 'D' || begin == 'd')
    	{
    		cout << "Please enter the input file name: ";
    		cin >> filename;
    		fin.open( filename.c_str() );
    
    		if(!fin)   //check for file validity
    		{
    			cout << "Can't open the input file" << endl;
    		}
    
    
    
    
    
    		fout.open("decrypted.txt");
    		//fout << 
    	}
    
    	return 0;
    }

    Comment

    • oler1s
      Recognized Expert Contributor
      • Aug 2007
      • 671

      #3
      Read in a line. Process that line. Repeat.

      Comment

      • Mighty Mackinac
        New Member
        • Oct 2008
        • 4

        #4
        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++

        Comment

        • Mighty Mackinac
          New Member
          • Oct 2008
          • 4

          #5
          what do you think about using maps? i saw a program that used them, thought that it would be easier

          Comment

          Working...