Thread: More newbie questions

  1. #1
    sunzoner
    Guest

    Question More newbie questions

    I have a problem with reading iofiles. I tried to create an array and output it to a file. Then input the array back into the program as variables.

    But I am unable to get the correct values back.

    Also, when I check the file, I realise only 1 value is written to the file.

    What can/should I do?

    Thanks in advance...

    The codes is here:
    #include <iostream.h>
    #include <fstream.h>
    #include <conio.h>

    int presskey();

    int main()
    {
    int x, y, anarray[1][1];
    cout << "Please enter a number. \n";
    cin >> x;
    cout << "Please enter another number. \n";
    cin >> y;
    ofstream a_file("arrayio.txt");
    anarray[1][1] = x, y;
    a_file << anarray[1][1];
    a_file.close();
    presskey();

    ifstream b_file("arrayio.txt");
    b_file >> anarray[1][1];
    cout << anarray[1][1];
    b_file.close();
    presskey();

    return 0;
    }

    int presskey()
    {
    cout << " Press Enter to continue... \n";
    getch();
    }

  2. #2
    Unregistered
    Guest
    You are declaring an array of 1x1, and that can hold only one value. Try using :
    int anarray[2];
    anarray[0] = x;
    anarray[1] = y;

  3. #3
    sunzoner
    Guest
    Thanks for your answer. But how do I store and read a two demisional array from a file?

  4. #4
    Unregistered
    Guest
    Try using write :
    Code:
    ofstream f("test.txt");
    int arr[3][4];
    f.write((char*)arr, sizeof(arr));
    To read from the file use read :
    Code:
    ifstream inFile("test.txt");
    int arr[3][4];
    inFile.read((char*)arr, sizeof(arr));

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory handling functions (newbie questions)
    By PaulBlay in forum C Programming
    Replies: 6
    Last Post: 03-10-2009, 06:37 AM
  2. newbie questions
    By raptorx in forum C Programming
    Replies: 2
    Last Post: 10-10-2007, 09:30 PM
  3. Im a newbie to C and i have some questions
    By pave1104 in forum C Programming
    Replies: 5
    Last Post: 07-05-2006, 09:48 PM
  4. Real newbie with VC6 questions
    By MagiZedd in forum Windows Programming
    Replies: 8
    Last Post: 10-15-2001, 08:27 PM
  5. newbie questions again
    By dune911 in forum C++ Programming
    Replies: 2
    Last Post: 09-14-2001, 02:43 PM