writing arrays to files

This is a discussion on writing arrays to files within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm wanting to be able to read and write two arrays to a file. Read the arrays when the ...

  1. #1
    Zaarin
    Guest

    writing arrays to files

    Hi,

    I'm wanting to be able to read and write two arrays to a file.

    Read the arrays when the program opens, and write the arrays when the program closes.

    The two arrays are:
    char Reg[15][7] = {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{ 0},{0},{0}};
    char Model[15][11] = {{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{ 0},{0},{0}};

    My problem is that my program dousn't correctly read and write to the file.

    When it writes to the file, it only writes two characters. It should write complete strings.

    My code for writing and reading to files are as follows (I am including fstream.h):
    =============================================
    ifstream inFile;
    inFile.open("C:\\parkeasy.dat", ios::in);


    if (!inFile.fail())

    {
    inFile >> Reg[15][7];
    inFile >> Model[15][7];

    inFile.close();
    }

    else
    {

    cout << "Error finding or creating file Carkeasy.dat" << endl;

    }
    =============================================

    ofstream outFile;
    outFile.open("C:\\parkeasy.dat", ios:ut);

    if(!outFile.fail())
    {
    outFile << Reg[15][7] << endl;
    outFile << Model[15][7] << endl;

    outFile.close();
    }
    else
    {
    cout << "error opening file" << endl;
    }
    ==============================================

    The following is an example of the arrays with strings:

    char Reg[15][7] = {{"394EIR"},{"238PNG"},{"138THX"},{"908BAD"},{0},{ 0},{"786GIF"},{0},{"678DOG"},{0},{0},{0},{0},{0},{ 0}};
    char Model[15][11] = {{"FALCON"},{"COMMODORE"},{"FAIRLANE"},{"121-METRO"},{0},{0},{"HILUX"},{0},{"SKYLINE"},{0},{0}, {0},{0},{0},{0}};


    Any help would be appreciated greatly.

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    inFile >> Reg[15][7];
    inFile >> Model[15][7];

    you must loop through the arrays

    if they are strings the following will do

    for(int i < 15; i++)
    {
    inFile >> reg[i];
    }


    same for Model..

    if they are characters or integers
    for(int i = 0 ; i< 15;i++)
    {
    for(int j=0;j<7;j++)
    {
    inFile << reg[i][j]
    }
    }

    //..same for the model
    zMan

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Global arrays shared between multiple source files
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 08-14-2008, 07:29 PM
  2. writing and reading files
    By oldie in forum C Programming
    Replies: 1
    Last Post: 07-03-2008, 05:54 PM
  3. C Programming Help - Using Arrays and Files
    By samdog45 in forum C Programming
    Replies: 1
    Last Post: 03-10-2008, 03:18 AM
  4. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 02:30 PM
  5. Replies: 0
    Last Post: 10-29-2001, 11:40 PM

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