Thread: New programmer here!

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    5

    New programmer here!

    Hello all,

    Does anyone know how to read a file, one character at the time, into an character array? No matter what I try I cant seem to get it to work.
    Edit:
    The size of the file is unknown...it will be different each time the program is run, and it could end up being very large.
    My main problem is putting the characters in an array when I don't know how many elements to define for the array.

    Here is the basic code i have:
    Code:
    int count;
    char *key="something here";
    FILE *in;
        if (( in = fopen("tmp.tmp", "rb")) == NULL) 
       {
          printf("Error opening %s.n", "");
       }
       while(( count = getc(in)) != EOF) 
       {
          count = count ^ *key; 
       //store here
       }
    }
    Last edited by Phob0s; 05-02-2006 at 04:10 PM.

  2. #2
    Registered User 00Sven's Avatar
    Join Date
    Feb 2006
    Posts
    127
    Code:
    int ch;
    char array[10];
    FILE *in;
    in = fopen("temp.tmp", "r");
    if(in == NULL)
    {
         printf("Error opening file!\n");
    }
    while( (ch = getc(in)) != EOF)
    {
         array[i] = ch;
         i++;
    }
    ~Sven
    Windows XP Home Edition - Dev-C++ 4.9.9.2
    Quote Originally Posted by "The C Programming Language" by Brian W. Kernignhan and Dennis M. Ritchie
    int fflush(FILE *stream)
    On an output stream, fflush causes any buffered but unwritten data to be written; On an input stream, the effect is undefined. It returns EOF for a write error, and zero otherwise. fflush(NULL) flushes all output streams.
    board.theprogrammingsite.com

  3. #3
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Phob0s
    Hello all,

    Does anyone know how to read a file, one character at the time, into an character array? No matter what I try I cant seem to get it to work.

    Here is the basic code i have:
    Code:
    int count;
    FILE *in;
        if (( in = fopen("tmp.tmp", "rb")) == NULL) 
       {
          printf("Error opening %s.n", "");
       }
       while(( count = getc(in)) != EOF) 
       {
          count = count ^ *key; 
       //store here
       }
    }

    Try these changes. You used a bad name (count)
    for the input value, key_pressed would be better.





    Code]
    int count;
    int realcount=0;
    char key[100];
    FILE *in;
    if (( in = fopen("tmp.tmp", "rb")) == NULL)
    {
    printf("Error opening %s.n", "");
    }
    while(( count = getc(in)) != EOF)
    {

    key[realcounter++]=count;
    if (realcounter>=99); break;


    //store here
    }
    fclose(in);



    Another possible change.


    while ( fscanf("%c",&count)!=EOF)
    while(( count = getc(in)) != EOF)

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    5
    the number of characters are different for each execution...so we can't set number of elements in the array to a constant.

  5. #5
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Not sure what you are saying.
    Make the array[1000000] then and see how long it takes to fill it.
    Do you know which key returns EOF?

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    5
    basically i need to read one charater from a file, perform xor(^) on the character, then append the result to an existing array. I need to do this until the end of file. The only problem is that I don't know how to find the number of characters in the file, and thus do not know how many elements to give the array.

  7. #7
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Sorry I though you were reading from a file, I find
    getc confusing cos usually when you read from a
    file you proceed the function call with an f, for
    example scanf, fscanf.
    Anyway just make the array large or you could
    possiblly find the file size (somehow) and then use
    something like malloc to allocate memory.
    Alternatively you could write the data to another
    file. The last option is probably best cos you don't
    lose the data when the program finishes.

  8. #8
    Linux User
    Join Date
    Nov 2005
    Location
    Bellingham, WA
    Posts
    35

    Detecting File Size

    include statements (at least the ones I am using I am not sure which one does it).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    Code Itself:
    Code:
    FILE *file = fopen("filename.txt", "rb")
    if (file == NULL)
    {
      perror("Error Opening File filename.txt");
    }
    fseek(file, 0, SEEK_END);
    long size=ftell(file);
    fseek(file, 0, SEEK_SET);
    char* storage = malloc(char) * size+1;
    This should alocate exactly enough memory for any file you try to open
    "Just as eating contrary to the inclination is injurious to the health,
    so study without desire spoils the memory, and it retains nothing that it takes in."


    - Leonardo De Vinci (1452-1519)

  9. #9
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    char* storage = malloc(char) * size+1;
    Try again.

    [And maybe consider C90, too.]
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  10. #10
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by tuxinator
    This should alocate exactly enough memory for any file you try to open
    Is there a way to read the file size directly so to speak,
    I mean I think it is stored in the dirctory/folder info somewhere.
    Also I would like to find the data of creation. Will start a thread on this.

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Registered User
    Join Date
    May 2006
    Posts
    5
    Quote Originally Posted by tuxinator
    include statements (at least the ones I am using I am not sure which one does it).
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    Code Itself:
    Code:
    FILE *file = fopen("filename.txt", "rb")
    if (file == NULL)
    {
      perror("Error Opening File filename.txt");
    }
    fseek(file, 0, SEEK_END);
    long size=ftell(file);
    fseek(file, 0, SEEK_SET);
    char* storage = malloc(char) * size+1;
    This should alocate exactly enough memory for any file you try to open
    Code:
    char* storage = malloc((char)) * (size+1);
    throws a syntax error: ')'

  13. #13
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    Quote Originally Posted by Phob0s
    Code:
    char* storage = malloc((char)) * (size+1);
    throws a syntax error: ')'

    Try char* storage = malloc(char) * (size+1);
    or char* storage = (malloc(char)) * (size+1);

  14. #14
    Registered User
    Join Date
    May 2006
    Posts
    5
    Tried both...no cigar

  15. #15
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    OK


    char * storage = malloc( sizeof(char) * (size+1) );

    Not sure what you are trying to do.
    Anyway I will have a Hamlet

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What game programmer should I be? need some advice.
    By m3rk in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 04-20-2009, 11:12 PM
  2. When are you REALLY a programmer?
    By m.mixon in forum C Programming
    Replies: 5
    Last Post: 07-19-2006, 09:08 PM
  3. Senior 3D Programmer - Moab Studios™ LLC
    By moab in forum Projects and Job Recruitment
    Replies: 0
    Last Post: 08-30-2005, 10:19 PM
  4. Me as a programmer?
    By Cheeze-It in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 03-31-2002, 06:19 PM
  5. I need to interview professional programmer.....please
    By incognito in forum C++ Programming
    Replies: 1
    Last Post: 01-05-2002, 02:46 PM