-
in this code im trying to assign a character to each element of the array myarray. I have where the error occurs marked below with the message.
Code:
int main()
{
FILE *fp;
char ch;
int i=0;
char *myarray[i];
if((fp=fopen("C:/text2.txt", "r")) == NULL) {
cout <<"Cannot open";
}
ch = getc(fp);
while(ch!=EOF) {
myarray[i] = ch; //error--"assignment to 'char *' from 'char' lacks a cast.
ch = getc(fp);
i++;
}
fp=fopen("C:/text.txt", "w+");
for(i=0;i<100;i++){
fprintf(fp,myarray[i]);
}
system("pause");
fclose(fp);
return 0;
}
Also how can I make a array resize so that it can automatically resize to the number of characters in a text file? Thanks
-
Replace
with
Code:
char *myarray = new char[i];
-
-
The reason for that is that you have to dynamically allocate memory for any data structure whose size is not known at design-time. If you knew that the string had to be, say, 100 characters long then you would be able to use "char JustAString[100];".
You can get a resizeable string if you use the STL string class. Just #include <cstring> and you can get a string class that works just like strings do in VB, but with + as the string concatenation operator instead of &.
-
Oh and if you want a general resizeable array class, you can use the STL vector template class. I'm not sure which header that's in though.
-
looks like im going to do some research on the STL. I have a book that contains a few chapters on it so i will probably read them. Thanks again ;)
-
Code:
#include <iostream>
#include <string>
#include <vector>
int main() {
vector<string> vsArray;
vsArray.push_back("Hello");
vsArray.push_back("World");
cout << vsArray[0].substr(2,2) << " " << vsArray[1] << endl;
return 0;
}
-
Code:
int alpha(char *p)
{
FILE *fp;
int i=0;
int c=0;
char *myarray = new char[i];
char ch;
if((fp=fopen(p, "r")) == NULL) {
MessageBox(NULL, "Cannot Open File", "Error", 0 + MB_ICONEXCLAMATION);
}
myarray[i] = getc(fp);
ch = getc(fp);
while(ch!=EOF) {
myarray[i] = ch;
ch = getc(fp);
i++;
c++;
}
fp = fopen("C:/Matt/text.txt", "w+");
for(i=0;i<c;i++){
fprintf(fp,&myarray[i]);
}
delete[] myarray;
fclose(fp);
return 0;
}
I get a illegal operation when I run this. Any suggestions?
:confused:
-
I used i as the element number and incremented it each time it looped. I set it equell to zero becasue at one time it was not starting at 0. The array myarray[i] is suppose to hold a character per element. The way I was figuring is that it starts by putting the first character in '0' then increments 'i' putting the next one in '1'.
-
You have to make sure you allocate enough space for the whole array. They don't automatically extend for you. Is there any way for you to know how big it needs to be when the function is called?
-
Well, if there was a way for me to read how many characters are in a text file,then I could. Or I could set it to a huge number but I would think it would waste alot of resources.
-
Aha...there's a way round that. Files are normally read in "chunks". You have a buffer of a set size (say 4096 bytes), and progressively read until it's read less than the buffer size.
-
Do you mean instead of reading character by character read it lines at a time? so for example I would set char *myarray = new char[4096]?
-
Even simpler. Just use char myarray[4096] and there's no clean-up required :)
If you take a look at _stat you can get the file size before you start and if you want you can load the whole file into memory (as long as it's not some obscene size).
-
I have one more error that I don't understand.
it says assignment to char from char lacks a cast. Why would you need a cast when your assigning a character to a character with the same data types?
-
Bizarre...never seen that one before! :confused:
It could be because ch was promoted to int when it received the return value from getc.
-
Well, ill play around with it and see what i can do. Im using Dev C++ and i've seen some bizarre error messages with it . I appreciate all your help!:)