Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 2nd, 2007, 9:41 AM   #1
Satans_Banjo
Newbie
 
Join Date: Oct 2006
Posts: 15
Rep Power: 0 Satans_Banjo is on a distinguished road
Parsing String Input From Files

Hi

I'm relatively new to most C concepts but I understand that strings can be stored as arrays of the type 'char'. However I'm not sure how you could actually get data from a text file, put all the characters into various slots of an array and then manipulate them from there

Basically what I'm trying to do is make a rudimentary encryption method so that a text file can be encrypted and decrypted using this C program. I was thinking of most likely using a two-dimensional array (matrix) and performing a columnar transposition on the file, then saving it, then reversing the process using a different C program

Any input would be appreciated
Satans_Banjo is offline   Reply With Quote
Old Jun 2nd, 2007, 9:56 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
See fgets. C strings ARE arrays of char, terminated with 0, so that part's done.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 9th, 2007, 2:02 AM   #3
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
Quote:
Originally Posted by Satans_Banjo View Post
Hi

I'm relatively new to most C concepts but I understand that strings can be stored as arrays of the type 'char'. However I'm not sure how you could actually get data from a text file, put all the characters into various slots of an array and then manipulate them from there

Basically what I'm trying to do is make a rudimentary encryption method so that a text file can be encrypted and decrypted using this C program. I was thinking of most likely using a two-dimensional array (matrix) and performing a columnar transposition on the file, then saving it, then reversing the process using a different C program

Any input would be appreciated
By "columnar transposition" you just mean you're switching around the values in certain indexes, right? Keep it simple. And just interested, but how do you plan to do this? With the same method every time, or randomly? Anyway...

I'm not sure what part of the process of getting data from a text file you're having trouble with. There are two ways (that I know of) to get the input from a file. You can look all of this info up but I'll post it for you anyway; sorry if I make any mistakes.

1. Redirect the std input stream. The standard input stream is connected to the keyboard by default. To redirect it to a file, type the name of your executable, if it is called a.out, then type

a.out < inputfilename.dat

2. You can also use what are called file pointers. look that up tho, I can't explain it concisely enough to help you.

btw, fgets can be used with either file pointers or the standard input stream.
Fall Back Son is offline   Reply With Quote
Old Jun 9th, 2007, 7:31 AM   #4
Satans_Banjo
Newbie
 
Join Date: Oct 2006
Posts: 15
Rep Power: 0 Satans_Banjo is on a distinguished road
Quote:
Originally Posted by Fall Back Son View Post
By "columnar transposition" you just mean you're switching around the values in certain indexes, right? Keep it simple. And just interested, but how do you plan to do this? With the same method every time, or randomly? Anyway...

I'm not sure what part of the process of getting data from a text file you're having trouble with. There are two ways (that I know of) to get the input from a file. You can look all of this info up but I'll post it for you anyway; sorry if I make any mistakes.

1. Redirect the std input stream. The standard input stream is connected to the keyboard by default. To redirect it to a file, type the name of your executable, if it is called a.out, then type

a.out < inputfilename.dat

2. You can also use what are called file pointers. look that up tho, I can't explain it concisely enough to help you.

btw, fgets can be used with either file pointers or the standard input stream.
Basically what I want to do is store the entire file as an array of chars and then use a loop to put the contents of that 1-dimensional array into a 2-dimensional array. Then I would output the contents of that array reading top to bottom into another text file

I've used file pointers in Delphi before - is this where a file pointer would move along a file and copy each character into the array?
Satans_Banjo is offline   Reply With Quote
Old Jun 9th, 2007, 8:03 AM   #5
Adak
Hobby Coder
 
Join Date: May 2006
Posts: 58
Rep Power: 0 Adak is an unknown quantity at this point
There are programs out there, (free), that would do the job that you're describing. Much easier, and with the included source code, you can learn how it works easier, and modify it to suit your needs.

Some are as simple as ROT-13, which simply shifts or ROTates the alphabet of the plaintext, to create the ciphertext, by 13 letters of the alphabet. Not secure, of course.

For a good deal more security, and learning more about encryption, why not google "Playfair"? Simple system, used in the last century.

Have fun!
Adak is offline   Reply With Quote
Old Jun 9th, 2007, 8:54 AM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Since any true array is an area of contiguous memory, you can treat it as an array of any dimensions. All you have to do is tell the compiler, so it can increment the subdimensions properly. Since a passed array is treated as a pointer, you always have to specify the maximum dimension.
#include <iostream>

using std::cout;
using std::cerr;
using std::cin;
using std::endl;

typedef char (*p2D) [4];
typedef char (*p3D) [3][3];

void Out2D (p2D theArray, unsigned size)
{
    for (unsigned i = 0; i < size; ++i)
    {
        for (unsigned j = 0; j < 4; ++j) 
            cout << theArray [i][j];
        cout << '\n';
    }
    cout << '\n';
}

void Out3D (p3D theArray, unsigned size)
{
    for (unsigned i = 0; i < size; ++i)
    {
        for (unsigned j = 0; j < 3; ++j)
        {
            for (unsigned k = 0; k < 3; ++k) cout << theArray [i][j][k];
            cout << '\n';
        }
        cout << '\n';
    }
    cout << endl;
}


int main (int argc, char* argv [])
{
    char baseArray [] = "abcdefghijklmnopqrstuvwxyz12";

    cout << "1D array:\n";
    for (unsigned i = 0; i < 28; ++i) cout << baseArray [i];
    cout << "\n\n";

    cout << "2D array:\n";
    Out2D ((p2D) baseArray, 7);
    cout << "3D array:\n";
    Out3D ((p3D) baseArray, 3);
    cout << endl;

    return 0;
}
Quote:
Originally Posted by Output
1D array:
abcdefghijklmnopqrstuvwxyz12

2D array:
abcd
efgh
ijkl
mnop
qrst
uvwx
yz12

3D array:
abc
def
ghi

jkl
mno
pqr

stu
vwx
yz1
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 9th, 2007, 8:56 AM   #7
Satans_Banjo
Newbie
 
Join Date: Oct 2006
Posts: 15
Rep Power: 0 Satans_Banjo is on a distinguished road
Quote:
Originally Posted by Adak View Post
There are programs out there, (free), that would do the job that you're describing. Much easier, and with the included source code, you can learn how it works easier, and modify it to suit your needs.

Some are as simple as ROT-13, which simply shifts or ROTates the alphabet of the plaintext, to create the ciphertext, by 13 letters of the alphabet. Not secure, of course.

For a good deal more security, and learning more about encryption, why not google "Playfair"? Simple system, used in the last century.

Have fun!
Cheers - I've been getting into cryptography and this 'playfair' cipher seems pretty clever

I've written something that seems like it should work but the output is coming out a bit odd. Here's what I've got:

#include <stdio.h>

int main()
{
	FILE *input_file;
	FILE *output_file;
	char input[21];
	char output[21];
	char matrix[4][5];
	int x, y, i;
	printf("Opening file...\n");
	input_file = fopen("input_file.txt", "r");
	if (input_file == NULL) {
		printf("Could not open %s\n", "input_file.txt");
		return 1;
	}
	else {
		fgets(input, 21, input_file);
		fclose(input_file);
	}
	for (i=0;i<21;i++)
	{
		printf("%c", input[i]);
	}
	printf("\n");
	printf("Encrypting...\n");
	for(y=0; y<=4; y++)
	{
		for(x=0; x<=4; x++)
		{
			matrix[x][y] = input[(y+4*x)];
		}
	}
	i = 0;
	for (y=0;y<=4;y++)
	{
		for (x=0;x<=4;x++)
		{
			output[i] = matrix[x][y];
			i = (i+1);
		}
	}
	for (i=0;i<21;i++)
	{
		printf("%c", output[i]);
	}
	printf("\n");
	return 0;
}

The problem is, when I set up the input as "Programming Forums" I get the output "PriFPranoromgrigm uFr". I worked the desired output out by hand and got "PriFmRanosomgr gm u "
Satans_Banjo is offline   Reply With Quote
Old Jun 9th, 2007, 10:24 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
	for(y=0; y<=4; y++)
	{
		for(x=0; x<=4; x++)
		{
			matrix[x][y] = input[(y+4*x)];
		}
	}
The loops imply 5 rows by 5 columns (0,1,2,3,4), yet the move from the input array implies 4 columns (y+4*x).
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Jun 9th, 2007, 10:44 AM   #9
Satans_Banjo
Newbie
 
Join Date: Oct 2006
Posts: 15
Rep Power: 0 Satans_Banjo is on a distinguished road
Quote:
Originally Posted by DaWei View Post
	for(y=0; y<=4; y++)
	{
		for(x=0; x<=4; x++)
		{
			matrix[x][y] = input[(y+4*x)];
		}
	}
The loops imply 5 rows by 5 columns (0,1,2,3,4), yet the move from the input array implies 4 columns (y+4*x).
Of course! Thanks a lot. Works now
Satans_Banjo is offline   Reply With Quote
Old Jun 15th, 2007, 3:18 AM   #10
Fall Back Son
Professional Programmer
 
Join Date: Oct 2006
Posts: 311
Rep Power: 3 Fall Back Son is on a distinguished road
if you want to copy the whole file, character by character, then look up the appropriate function in the manual. getchar maybe? Maybe not. Try to use the function that best serves your purpose. A lot of times there are numerous functions you could use but one will be easier to use. That could be fgets, you would know better than me. It seems like you already have your question answered so I'm just throwing that out there. It's something I learned the hard way, although it looks like the way you did your program worked fine so maybe this won't be of use to you. .

http://www.cs.umbc.edu/courses/under...tures/streams/
Fall Back Son is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
An Attempt at a DBMS grimpirate PHP 8 Apr 17th, 2007 2:01 PM
Function Parameters grimpirate PHP 10 Mar 14th, 2007 7:55 PM
C# corruption!!! Kilo C++ 32 May 21st, 2006 9:44 PM
Array issues :( Alo Tsum Java 10 Nov 26th, 2005 6:45 PM
replace space with ' * ' TecBrain C++ 15 Apr 13th, 2005 1:32 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:22 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC