Thread: Reading from files

  1. #1
    Unregistered
    Guest

    Unhappy Reading from files

    If I have text-file wit 3 lines of text, how can I read that text into a char array and skip newlines?

    Example:

    myFile.txt:
    abcdef
    ghijklm
    opqrst

    myArray:
    abcdefghijklmopqrst

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Use the function fgetc to read all characters from the file and put all characters in the array one by one.

  3. #3
    Unregistered
    Guest
    stream version:

    char ch;
    char array[256];
    int i = 0;
    ifstream fin ("yourfilename.yourfilenameextension");

    //read in first cbar
    fin >> ch;

    //evaluate each char one by one
    while(ch != EOF)
    {
    if(ch != '\n')
    {
    //if not EOF or newline the put char in first available array element
    array[i++] = ch;
    }

    //get next char
    fin >> ch;
    }

    //make array a strring by adding null terminating char
    array[i] = '\0';

    //dispaly array
    cout << array << endl;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 AM
  2. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  3. reading files
    By hiya in forum C++ Programming
    Replies: 7
    Last Post: 05-21-2005, 11:40 AM
  4. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM