Results 1 to 17 of 17

Thread: arrays

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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

  2. #2
    Frenzied Member Vlatko's Avatar
    Join Date
    Aug 2000
    Location
    Skopje, Macedonia
    Posts
    1,409
    Replace
    Code:
    char *myarray[i];
    with
    Code:
    char *myarray = new char[i];
    I am become death, the destroyer of worlds.
    mail:[email protected]

    • Visual Basic 6.0 & .NET
    • Visual C++ 6.0 & .NET
    • ASP
    • LISP
    • PROLOG
    • C
    • Pascal

  3. #3

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    thanks Vlatko
    Matt

  4. #4
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  5. #5
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    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."

  6. #6

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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

  7. #7
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  8. #8

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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

  9. #9

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  11. #11

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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

  12. #12
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  13. #13

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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

  14. #14
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  15. #15

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    I have one more error that I don't understand.

    Code:
    myarray[i] = ch;
    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

  16. #16
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    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

  17. #17

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330
    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
  •  



Click Here to Expand Forum to Full Width