Read File into 2D char array...?
Page 1 of 111 Replies - 4836 Views - Last Post: 09 December 2009 - 11:14 AM
#1
Read File into 2D char array...?
Posted 09 December 2009 - 06:46 AM
Ok so this has been annoying me for an insane amount of hours...
Could someone please tell me how to read a file into a 2D (char) array?
Or even still, how to easily read in different lines from a file, or use the file as a 2D (char) array?
It's a (very basic) part of an assignment I have to do... but I just simply can not find a single thing online or in books about how to do this.
I don't have any code (sorry) because I simply just don't know how to do this, there's no other way to say it.
Could someone please tell me how to read a file into a 2D (char) array?
Or even still, how to easily read in different lines from a file, or use the file as a 2D (char) array?
It's a (very basic) part of an assignment I have to do... but I just simply can not find a single thing online or in books about how to do this.
I don't have any code (sorry) because I simply just don't know how to do this, there's no other way to say it.
Replies To: Read File into 2D char array...?
#2
Re: Read File into 2D char array...?
Posted 09 December 2009 - 07:31 AM
why reading a file to a 2d array? I can't find any good reason for that, there are a couple ways to do that but you have to specify C or C++ ? And what is your problem with it? Explain what is making you confused so that we can help, we can't just write you the code
This post has been edited by Anarion: 09 December 2009 - 07:32 AM
#3
Re: Read File into 2D char array...?
Posted 09 December 2009 - 07:45 AM
Anarion, on 9 Dec, 2009 - 06:31 AM, said:
why reading a file to a 2d array? I can't find any good reason for that, there are a couple ways to do that but you have to specify C or C++ ? And what is your problem with it? Explain what is making you confused so that we can help, we can't just write you the code 
Sorry, it's in C.
Ok basically the assignment is that we have to create a program that searches an email for a certain word. It's a spam program, so it also has to check for mutations of a certain word. Eg. viagra could be written as v1@grA etc.
So what I want (and have to) do is to store the mutations in a 2D char array, and their position in the array will be related to the decimal value of the character.
sooo it's be a bit like this:
Position,mutations
97 A@
.
.
.
105 I!l1
So I was hoping I could put these mutations into a file, and then the line number that these mutations are on could relate to the decimal value of the character.
And then in my program, I could just use that file to check for mutations..... or else, copy the file into a 2D array (the row number being the dec. value of the character, the columns being filled with the mutations).
Soo yeah, I basically can not find information anywhere on how to access a character at a certain row and certain position of a file that is being read in.
I know how to read a file, where I have a pointer pointing at the file. But that seems to only work for the first row of characters.
#5
Re: Read File into 2D char array...?
Posted 09 December 2009 - 08:42 AM
Super thanks...
But how would one change the position so that it goes to the next line? As far as I see from that, and from running it, it's only possible to set the position when the program gets to that position.
But how would one change the position so that it goes to the next line? As far as I see from that, and from running it, it's only possible to set the position when the program gets to that position.
#7
Re: Read File into 2D char array...?
Posted 09 December 2009 - 10:00 AM
Ok, I decided I'm not going to bother with the file thing because it's just far too much work and too confusing, having to work out bits and bytes and all that.
Sooo I decided to just use a 2D char array. I just have one question (I'll ask here instead of starting a new topic). How do you pass a 2D array to a function?
This is a summary of the relevant code I have done:
I've tried all the ways I can think of for passing the 2D array to the function, but I keep getting an error.
So could someone please tell me how I should write the 2D array so I can pass it to the function?
Sooo I decided to just use a 2D char array. I just have one question (I'll ask here instead of starting a new topic). How do you pass a 2D array to a function?
This is a summary of the relevant code I have done:
int compare_character(char spamchar,char emailchar, char *mutations[]){
}
main(){
char mutations[26][5] = {
{'a','A','@'},
{'b','B'},
{'c','C'},
{'d','D'},
{'e','E','3'},
{'f','F'},
{'g','G'},
{'h','H'},
{'i','I','1','l','!'},
{'j','J'},
{'k','K'},
{'l','L','I'},
{'m','M'},
{'n','N'},
{'o','O','0'},
{'p','P'},
{'q','Q'},
{'r','R'},
{'s','S'},
{'t','T'},
{'u','U'},
{'v','V'},
{'w','W'},
{'x','X'},
{'y','Y'},
{'z','Z'}
};
if(compare_character('a','a',*mutations) == 1){
printf("1");
}
else{
printf("0");
}
return 0;
}
I've tried all the ways I can think of for passing the 2D array to the function, but I keep getting an error.
So could someone please tell me how I should write the 2D array so I can pass it to the function?
This post has been edited by Mark200: 09 December 2009 - 10:02 AM
#8
Re: Read File into 2D char array...?
Posted 09 December 2009 - 10:10 AM
you need to tell the function the size of the 2nd dimension, first's size is optional
first one is more appropriate since you declared it like that char[][]
void function(char arr[][10]){
}
//or
void function(char* arr[10]){
}
//or
void function(char** arr){
}
first one is more appropriate since you declared it like that char[][]
This post has been edited by KYA: 09 December 2009 - 10:10 AM
#9
Re: Read File into 2D char array...?
Posted 09 December 2009 - 10:21 AM
I'm still getting an error 
I now have the function like this:
Maybe I'm passing the arguments incorrectly?
Is that right?
The error I'm getting is:
And it's for the line where I call the function and the line where I declare it.
I now have the function like this:
int compare_character(char spamchar,char emailchar, char mutations[][5]){
}
Maybe I'm passing the arguments incorrectly?
compare_character('a','a',*mutations)
Is that right?
The error I'm getting is:
Quote
warning: passing argument 3 of âcompare_characterâ from incompatible pointer type
pe
pe
And it's for the line where I call the function and the line where I declare it.
This post has been edited by Mark200: 09 December 2009 - 10:23 AM
#10
Re: Read File into 2D char array...?
Posted 09 December 2009 - 10:33 AM
Just pass the name of it. Dereference isn't needed here.
Example;
Example;
//first demin size isn't necessary, but since you know it ahead of time...
int compareCharacters(char spam, char email, char arr[26][5]){
//do stuff
return 1;
}
int main(){
char stuff[26][5];
compareCharacters('a', 'b', stuff);
return 0;
}
#11
Re: Read File into 2D char array...?
Posted 09 December 2009 - 10:46 AM
Super, first time I've gotten no errors in hours!
Thanks a lot
Thanks a lot
#12
Re: Read File into 2D char array...?
Posted 09 December 2009 - 11:14 AM
Wait, you can create a 2d vector and push all your data in it, if you were still trying to figure that out.
#include <vector> // include this
ifstream in("DATA.txt");
//create 2d vector
vector< vector<char> >stuff(0);
//create a temp row vector
vector<char>temp(0);
char c = ' ';
//read file char by char
while(in.get(c))
{
temp.push_back(c);
if(c == '\n')
{
//push each row into the 2d vector
stuff.push_back(temp);
//clear the vector
temp.clear();
}
}
//output 2d vector and look inside
for(int i=0; i<stuff.size(); i++)
for(int k =0; k<stuff[i].size(); k++)
cout<<stuff[i][k];
//close file
in.close();
Page 1 of 1
|
|

New Topic/Question
Reply



MultiQuote




|