|
-
Apr 15th, 2001, 06:13 PM
#1
Thread Starter
Hyperactive Member
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
Matt 
-
Apr 15th, 2001, 06:21 PM
#2
Frenzied Member
Replace
with
Code:
char *myarray = new char[i];
-
Apr 15th, 2001, 06:35 PM
#3
Thread Starter
Hyperactive Member
thanks Vlatko
Matt 
-
Apr 15th, 2001, 08:00 PM
#4
Frenzied Member
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 &.
Harry.
"From one thing, know ten thousand things."
-
Apr 15th, 2001, 08:02 PM
#5
Frenzied Member
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.
Harry.
"From one thing, know ten thousand things."
-
Apr 15th, 2001, 10:16 PM
#6
Thread Starter
Hyperactive Member
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
Matt 
-
Apr 16th, 2001, 12:07 PM
#7
Monday Morning Lunatic
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;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 16th, 2001, 07:37 PM
#8
Thread Starter
Hyperactive Member
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?
Matt 
-
Apr 17th, 2001, 09:45 AM
#9
Thread Starter
Hyperactive Member
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'.
Last edited by MPrestonf12; Apr 17th, 2001 at 09:51 AM.
Matt 
-
Apr 17th, 2001, 11:55 AM
#10
Monday Morning Lunatic
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?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 17th, 2001, 12:01 PM
#11
Thread Starter
Hyperactive Member
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.
Matt 
-
Apr 17th, 2001, 12:04 PM
#12
Monday Morning Lunatic
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.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 17th, 2001, 12:08 PM
#13
Thread Starter
Hyperactive Member
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]?
Matt 
-
Apr 17th, 2001, 12:10 PM
#14
Monday Morning Lunatic
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 refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 17th, 2001, 12:34 PM
#15
Thread Starter
Hyperactive Member
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?
Matt 
-
Apr 17th, 2001, 12:37 PM
#16
Monday Morning Lunatic
Bizarre...never seen that one before! 
It could be because ch was promoted to int when it received the return value from getc.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Apr 17th, 2001, 12:41 PM
#17
Thread Starter
Hyperactive Member
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!
Matt 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|