Thread: Array problem - Newbie

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    43

    Smile Array problem - Newbie

    I have written the piece of code below.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    main(){
    
    // open the file
    FILE*fp;
    int letter;
    if((fp=fopen("BMPHTM2.HTM","r"))==NULL)
    {
    puts("Cannot open the file");
    }
    
    //declarations
    int i=0;
    char z[2000];
    int j;
    int k;
    char a[100];
    char b[100];
    char c[100];
    char d[100];
    char e[100];
    char f[100];
    char g[100];
    
    //read each char from file
    while((letter=fgetc(fp)) !=EOF)
    {
    printf("%c",letter);
    z[i] = letter;
    i++;
    }
    fclose(fp);
    
    //full range is j=158 to j<444 (41 per line?)
    k=0;
    for (j=158; j<199; j++){
    a[k]=z[j];
    k++;
    }
    k=0;
    for (j=200; j<240; j++){
    b[k]=z[j];
    k++;
    }
    k=0;
    for (j=241; j<281; j++){
    c[k]=z[j];
    k++;
    }
    k=0;
    for (j=282; j<322; j++){
    d[k]=z[j];
    k++;
    }
    k=0;
    for (j=323; j<363; j++){
    e[k]=z[j];
    k++;
    }
    k=0;
    for (j=364; j<404; j++){
    f[k]=z[j];
    k++;
    }
    k=0;
    for (j=405; j<444; j++){
    g[k]=z[j];
    k++;
    }
    
    printf("%s", z);
    printf("%s", a);
    printf("%s", b);
    printf("%s", c);
    printf("%s", d);
    printf("%s", e);
    printf("%s", f);
    printf("%s", g);
    return 0;
    }
    And in this file is the output from my program:

    http://cboard.cprogramming.com/attac...tid=5160&stc=1

    As you can see I am getting phantom Unicode characters which are distorting the final output.

    What is causing these random characters to be inserted, and how do I stop this from happening?

    Thankyou for any help.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You're not null-terminating your strings.

    Also, all variable declarations have to go at the beginning of a code block or outside any function (i.e. global). Your code won't even compile with my compiler because of the non-standard place in which your variables are declared.
    Last edited by itsme86; 01-04-2005 at 09:33 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    int main()
    {
    You should explicitly state that main returns an int.

    Code:
    FILE*fp;
    int letter;
    if((fp=fopen("BMPHTM2.HTM","r"))==NULL){
    puts("Cannot open the file");
    return 0;
    }
    
    //read each char from file
    while((letter=fgetc(fp)) !=EOF)
    {
    printf("%c",letter);
    z[i ] = letter;
    i++;
    }
    If your code fails to open the file, it would still go to the while loop and attempt to get characters from the file. You probably want to add a return statement as I have done in blue above.

    Code:
    char z[2000];
    
    while((letter=fgetc(fp)) !=EOF  && i < (sizeof(z)-1))
    {
    printf("%c",letter);
    z[i ] = letter;
    i++;
    }
    z[i ] = 0;
    Are you always certain that you will be reading in less than 2000 characters? A simple change in blue above will take care of that issue. The -1 part is because you are printing out these character arrays at the end of your program as character strings and you will need to add the NULL terminating character to the end of the characters you read in... so you should only read in at most 1999 characters and save the last one for the NULL. This is also done in blue above.

    Code:
    k=0;
    for (j=158; j<199; j++){
    a[k]=z[j];
    k++;
    }
    a[k] = 0;
    All of these arrays (a-g) need to be NULL terminated since you are printing them out as character strings later in your code. This is done in blue in the code above for the first array a.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Dec 2004
    Posts
    43
    Thank you

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie array problem
    By Andy123 in forum C Programming
    Replies: 3
    Last Post: 05-17-2006, 05:46 AM
  2. help w/ array problem
    By rhythm313 in forum C++ Programming
    Replies: 3
    Last Post: 11-12-2002, 12:12 AM
  3. problem: reading user input into an array
    By alpha561 in forum C Programming
    Replies: 13
    Last Post: 05-24-2002, 07:23 PM
  4. Comparing a 2d Array (newbie)
    By Cockney in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2001, 12:15 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM