![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Oct 2006
Posts: 15
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 | |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
Quote:
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. |
|
|
|
|
|
|
#4 | |
|
Newbie
Join Date: Oct 2006
Posts: 15
Rep Power: 0
![]() |
Quote:
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? |
|
|
|
|
|
|
#5 |
|
Hobby Coder
Join Date: May 2006
Posts: 59
Rep Power: 0
![]() |
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! |
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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:
__________________
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 |
|
|
|
|
|
|
#7 | |
|
Newbie
Join Date: Oct 2006
Posts: 15
Rep Power: 0
![]() |
Quote:
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 " |
|
|
|
|
|
|
#8 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
for(y=0; y<=4; y++)
{
for(x=0; x<=4; x++)
{
matrix[x][y] = input[(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 |
|
|
|
|
|
#9 | |
|
Newbie
Join Date: Oct 2006
Posts: 15
Rep Power: 0
![]() |
Quote:
|
|
|
|
|
|
|
#10 |
|
Professional Programmer
Join Date: Oct 2006
Posts: 311
Rep Power: 3
![]() |
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/ |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |