Array-File

This is a discussion on Array-File within the C Programming forums, part of the General Programming Boards category; Hello, can anyone help me with this code? I know that the problem is caused by the array, but I ...

  1. #1
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20

    Array-File

    Hello, can anyone help me with this code? I know that the problem is caused by the array, but I donīt know how to solve it.

    Code:
    //---------------------------------------------------------------------------
    
    #pragma hdrstop
    
    //---------------------------------------------------------------------------
    
    #include <stdio.h>
    #pragma argsused
    void main(int argc, char* argv[])
    {
    char textov[255];
    FILE *archivo;
    archivo= fopen("c:\\ecer.txt", "r");
    
    if (archivo == NULL){
    printf("El archivo ecer.txt no se encuentra en el directorio adecuado, o no existe");
    }
    else{
    printf("El archivo existe");
    fscanf(archivo,"%c", &textov);
    }
    
    printf("%c", textov);
    getch();
    
    }
    The array only keep one letter("ę"), and is a wrong letter, because it is not in ecer.txt. I thnik that I am wrong reading the text and keeping it in the array, can anyone help me?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void main
    Stop using void main. Read the FAQ.
    Code:
    fscanf(archivo,"%c", &textov);
    You don't need the & because the name of an array is treated in this case as a pointer to it. Also, it is only scanning one character, because that's all you're telling it to scan:
    Code:
    fscanf(archivo,"%c"

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    You are only reading in a single character into the first array location of textov, then attempting to print it (as it is not NULL terminated, you get undefined (and unexpected) results). Change your code to this:

    Code:
    fscanf(archivo,"%s", textov);
    }
    
    printf("%s", textov);
    Note that your use of fscanf here is just waiting to get a buffer overflow, you should set limits to the input thus:

    fscanf(archivo,"%254s", textov);

    Note that the size limit is one less than the length of the array, you must leave room for the NULL termination.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  4. #4
    Registered User Bitojis's Avatar
    Join Date
    Jun 2005
    Posts
    20
    It works untill ther is a space. In the .txt file I have this text "Hello, how are you?", and only prints "Hello,". There is a problem with the spaces, but I donīt know which is.

    Thanks,

  5. #5
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    That is because scanf works on tokens. Have you RTFM on scanf? I am sure the FAQ also covers this subject.

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21