0

I am working on a program that will read the first number of a file, create an array of that size, then read a second file, then create another array of that size. It will then fill the first array with values from the first file, then fill the second array with values from the second file. I have most of it worked out, but I can't figure out how to grab that first number in a file. Can someone help me?

int main()
{

    //int a[][],b[][],i,j,k,s,r;

    ifstream inFile1,inFile2;
    inFile1.open ("mat1.txt");
    inFile2.open ("mat2.txt");

    inFile1 >> n;
    for(i=0;i<n;++i)
    for(j=i;j<n;++j)
    inFile1 >> a[i][j];

    inFile2 >> n;
    for(i=0;i<n;++i)
    for(j=i;j<n;++j)
    inFile2 >> b[i][j];


    inFile1.close();
    inFile2.close();
    return 0;
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203

2 Answers2

0

This is how you read from a file, you can fiddle with this to read the entire file or just part of it:

fgets is what used to read from a file

   const int maxString = 1024;  // put a limit for your buffer

   printf("reading file\n");
    char buf[maxString];  /* make a buffer to store what your reading from 
                            the file*/
    FILE * fr = fopen(fn, "r");  // open the file
    while(fgets(buf, maxString, fr))  /* fgets reads the file and writes it 
                                         to buffer*/
    {
        fputs(buf, stdout); /*see what it is reading*/
    }

    fclose(fr); // close the file
    remove(fn); // if you want to remove the file when your done reading

or maybe do something like this:

string line;
 //while there is a line to get
while( getline( fileIn, line))
{
  firstCharacter = line[0]; // access the first character
}
BlooB
  • 955
  • 10
  • 23
0

I wasn't totally clear what you wanted from the question, but I'm assuming you want to read in an n x n array from two different files, where n is not known until the file has been opened. The code below should do that.

Note the use of new to declare an array of a size which is only known at run-time, and the slightly clumsy indexing i*n1 + j etc. This is because the c++ standard (or at least the earlier ones) don't support Variable Length Arrays... it turns out they work fine on my compiler, but I understand there is no guarantee they will work on all compilers (see here). An alternative would be to use vectors rather than arrays.

Also there's a lot of repetition in this code so you'd probably be better of putting repeated stuff in a function.

/*
// mat1.txt
3
1 2 3
4 5 6
7 8 9
*/

/*
// mat2.txt
4
1  2  3  4
5  6  7  8
9  10 11 12
13 14 15 16
*/

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int n1, n2;

    ifstream inFile1,inFile2;
    inFile1.open ("mat1.txt");
    inFile2.open ("mat2.txt");

    inFile1 >> n1;
    int* file1contents = new int[n1*n1];
    for(int i=0; i<n1; ++i) {
        for(int j=0; j<n1; ++j) {
            inFile1 >> file1contents[i*n1 + j];
        }
    }

    inFile2 >> n2;
    int* file2contents = new int[n2*n2];
    for(int i=0; i<n2; ++i) {
        for(int j=0; j<n2; ++j) {
            inFile2 >> file2contents[i*n2 + j];
        }
    }

    // Print file contents to check it worked
    cout << "File 1, size: " << n1 << ", contents:" << endl;
    for(int i=0; i<n1; ++i) {
        for(int j=0; j<n1; ++j) {
            cout << file1contents[i*n1 + j] << ",";
        }
        cout << "\n";
    }
    cout << endl;

    cout << "File 2, size: " << n2 << ", contents:" << endl;
    for(int i=0; i<n2; ++i) {
        for(int j=0; j<n2; ++j) {
            cout << file2contents[i*n2 + j] << ",";
        }
        cout << "\n";
    }
    cout << endl;

    delete [] file1contents;
    delete [] file2contents;

    inFile1.close();
    inFile2.close();
    return 0;
}
Community
  • 1
  • 1
kabdulla
  • 5,199
  • 3
  • 17
  • 30