Thread: How to read from a file into an array or pointer

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    1

    How to read from a file into an array or pointer

    Hi,

    I have a file on my system which I want to read and store into a pointer variable *int.

    This is my code:

    FILE * f = fopen ("C:\Dokumente und Einstellungen\Ralfie.MEIRSMAN-232FF2\Desktop\http-get\msvc2008\Debug\Ebay","rb");
    int * file = 0;
    int * file_begin = file;
    while ((*file = getchar()) != EOF){
    file++;
    }
    printf ("Content of file Ebay : %s", file_begin);


    int * file is the pointer variable where the content of the file should be stored in.
    int * file_begin points to the beginning of the string.

    The code compiles well but when executing the code I'm getting in a sort of endless loop. The printf command is never reached.

    Can somebody come up with a solution for this trivial problem?

    Thanks already.

    Ralf.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    1) You have a pointer variable, but it doesn't point anywhere. If you want to write through the pointer, you should make it point somewhere.

    2) Were you planning to read from your file at some point?

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Is this supposed to be C++? ...Or C?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Read file into array
    By neobarn in forum C Programming
    Replies: 6
    Last Post: 01-09-2009, 12:41 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. read from file into Array
    By tinus in forum C Programming
    Replies: 11
    Last Post: 04-13-2007, 07:11 AM
  5. Replies: 1
    Last Post: 09-10-2005, 06:02 AM