Thread: File to array?

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    24

    File to array?

    Look at the function HandleEncrypt()... The look even closer at the while loop that is there... What am i doing wrong here. I am trying to open a file then read the contents of the file to Contents[] so that i can later change the contents. Then i am printing the contents to the screen... The program compiles fine but that one line that i have remarked is causing me problems... I think im using m array worng..

    Code:
    #include <stdio.h> 
    #include <stdlib.h> 
    #include <time.h> 
    
    struct Encrypt{ 
       char *Password; 
       int   RandomVal; 
    }EnCryptoGraph; 
    
    struct Decrypt{ 
       char *Password; 
       int   RandomVal; 
    }DeCryptoGraph; 
    
    int  OpenFile(char *Path, char *RW); 
    int  RandomNum(void); 
    
    void HandleMenuChoice(void); 
    void HandleEncrypt(void); 
    void HandleDecrypt(void); 
    
    
    char *FilePath, Contents[] = "Testing"; 
    FILE *fp; 
    
    int main(void){ 
    
    srand( (unsigned)time( NULL ) ); /****** SEEDING rand() FOR CRYPTIC CODE ******/ 
    
    printf("***** ***** *   * ***** ******* ***** ******* ***** *     * *******\n"); 
    printf("*     *   *  * *  *   *    *    *   *    *    *      *   *     *\n"); 
    printf("*     ****    *   *****    *    *   *    *    ****     *       *\n"); 
    printf("*     *   *   *   *        *    *   *    *    *      *   *     *\n"); 
    printf("***** *   *   *   *        *    *****    *    *****  *   *     *\n"); 
    
    /* ***********Main Menu********** */ 
    
    printf("\n1) Encrypt\n"); 
    printf("2) Decrypt\n"); 
    
    printf("\nEnter a Number: "); 
    HandleMenuChoice(); 
    
    
    
    
    
    
    return 0; 
    } 
    
    void HandleMenuChoice(void){ 
       int ch; 
    
       while (1){ 
          ch = getchar(); 
             if(ch == 49){ 
                HandleEncrypt(); 
                break; 
             } 
             else{ 
                if (ch == 50){ 
                   HandleDecrypt(); 
                   break; 
                }else if(ch != '\0') break; 
             } 
             printf("\nInvalid Entry\n Please Enter a Number(1-2):");       
       } 
    } 
    
    
    /* *********Handle Encryption********* */ 
    
    void HandleEncrypt(void){ 
           
          char c; 
          int cnt; 
    
          printf("You Pressed 1\n"); 
          printf("%d\n",RandomNum()); 
    
          printf("Type the Path and File Name of your .txt file:\n"); 
                scanf("%s", &FilePath); 
                printf("%s\n",&FilePath); 
                 
                OpenFile("c:\\Testing.txt","r+"); 
                printf("c is %d",(c = fgetc(fp))); 
                  
                while ( ( c = fgetc(fp)) != '\0' ){ 
                      Contents[cnt] = c;             /* Problem Here */ 
                      putchar(c); 
                      cnt++; 
                       
                   } 
                 
       } 
    
    /* *********Handle Decryption********* */ 
    
    void HandleDecrypt(void){ 
          printf("You Pressed 2\n"); 
          printf("%d\n",RandomNum()); 
    
       } 
    
    /* ******Generate Random Number For Encryption Code****** */ 
    
    int RandomNum(){ 
    
       int num; 
    
       num = rand()%9; 
    
    return num; 
    } 
    
    /* ***** Open Binary File ***** */ 
    
    int OpenFile(char *Path, char *RW){ 
    
    
    
          if ( (fp = fopen(Path, RW)) == NULL) 
          { 
             fprintf(stderr, "Error opening file."); 
          }else printf("File Open\n"); 
    return 0; 
    }

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    cnt is never set to zero!

  3. #3
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>char Contents[] = "Testing";
    and then:
    >>Contents[cnt] = c;
    What are you trying to achieve here? You're going to overrun that buffer real quick.

    >>OpenFile()
    This function returns 0 for both success and failure. Not a particularly good plan, imo

    Try to limit your global variables too. There's no real need for some of those to be global.

    >>( c = fgetc(fp)) != '\0' )
    You'd do well to check for EOF. I doubt your text file has a a 0x00 byte in it (maybe it has, I dunno!)

    >>OpenFile("c:\\Testing.txt","r+");
    If you're not going to write to the file, use read only mode, it'll be safer.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    24
    thanx for all your help.... i got it to work. i failed to set a few variables so that was causing me problems...


    ________
    www.CBeginnersUnited.com -For all your nebie questions and for free beginner lectures LIVE on irc!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM