| | |
Inputting text file data into an array, please help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2006
Posts: 12
Reputation:
Solved Threads: 0
Hi guys and girls, im currently struggling trying to work out all this array and file input stuff!
I have been going round in circles for the past few hours, reading books and googling everything i could, so now im hoping some of your intelligence will help me
Basically i have a text file, containing names each with 12 numbers underneath them:
Bob Smith
12 17 84 93 83 48 93 47 95 28 84 26
as an example
I need to take the first 6 numbers and put them into an array, and put the last 6 into a different array.
I'm struggling badly, here's what i have done:
The attempt at an array does nothing and was the result of trial and error from all the reading i have been doing, but now im on the verge of throwing my monitor out of the window.
Any help very much appreciated, apologies for any spelling mistakes!
I have been going round in circles for the past few hours, reading books and googling everything i could, so now im hoping some of your intelligence will help me

Basically i have a text file, containing names each with 12 numbers underneath them:
Bob Smith
12 17 84 93 83 48 93 47 95 28 84 26
as an example

I need to take the first 6 numbers and put them into an array, and put the last 6 into a different array.
I'm struggling badly, here's what i have done:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <string> #include <fstream> using namespace std; bool getInputFilename(char fname[]) { ifstream fin; cout << "Please enter the filename for input : "; cin >> fname; fin.open(fname, ios::nocreate); if (!fin.is_open()) return false; fin.close(); return true; } bool getOutputFilename(char fname[]) { ofstream fout; cout << "Please enter the filename for output : "; cin >> fname; fout.open(fname); if (fout.fail()) return false; fout.close(); return true; } int firstArray(int i, int array[6], int maxSize, int amountRead) { ifstream fin; ofstream fout; while(fin>>array[amountRead]&& amountRead < maxSize) { amountRead++; } for (i=0; i < 5; i++) { fout << array[i] << endl; } cin.get(); return 0; } void main() { ifstream fin; ofstream fout; char IFname[20], OFname[20]; while (!getInputFilename(IFname)) { cout << "Invalid filename try again!\n\n"; } while (!getOutputFilename(OFname)) { cout << "Invalid filename try again!\n\n"; } fout.open(OFname); fin.open(IFname); }
The attempt at an array does nothing and was the result of trial and error from all the reading i have been doing, but now im on the verge of throwing my monitor out of the window.
Any help very much appreciated, apologies for any spelling mistakes!
it does nothing because the function is never called anywhere. And why not use std::string for the file names instead of C style character arrays? If you are writing c++ then use c++ as much as possible. And you should use getline() for entering filenames so that the path and file names can contain spaces.
•
•
Join Date: Jan 2006
Posts: 12
Reputation:
Solved Threads: 0
Well i had a play about and changed a few things to no avail. As for the C style character arrays because it was how i was taught...
Output file ends up giving me some random numbers (0012FE00)
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <string> #include <fstream> using namespace std; bool getInputFilename(char fname[]) { ifstream fin; cout << "Please enter the filename for input : "; cin >> fname; fin.open(fname, ios::nocreate); if (!fin.is_open()) return false; fin.close(); return true; } bool getOutputFilename(char fname[]) { ofstream fout; cout << "Please enter the filename for output : "; cin >> fname; fout.open(fname); if (fout.fail()) return false; fout.close(); return true; } void firstArray(char name[], int marks []) { ifstream fin; ofstream fout; while (!fin.eof()) { for (int i = 0; i < 12; i++) { fin >> marks[i]; } } } void main() { ifstream fin; ofstream fout; char IFname[20], OFname[20]; int marks [12]; char name[40]; firstArray(name, marks); while (!getInputFilename(IFname)) { cout << "Invalid filename try again!\n\n"; } while (!getOutputFilename(OFname)) { cout << "Invalid filename try again!\n\n"; } fout.open(OFname); fin.open(IFname); fout << marks << endl; fout.close(); fin.close(); }
Output file ends up giving me some random numbers (0012FE00)
In function firstArray() you declared an input stream and an output stream. The output stream is never used, so you might as well delete it. The input stream is never opened, so the loop will always fail.
main() is already opening the input and output streams, so why not just pass the name of the input stream as another parameter to firstArray() function and delete both those stream objects you declared inside that funtion. You will also have to change the code shown in blue below.
Then in main(), move that function call to firstArray down after the files are opened, and pass the parameters as shown above.
After opening the files you need to make a sanity check to see if the files were opened ok. Something like this
main() is already opening the input and output streams, so why not just pass the name of the input stream as another parameter to firstArray() function and delete both those stream objects you declared inside that funtion. You will also have to change the code shown in blue below.
C++ Syntax (Toggle Plain Text)
void firstArray(ifstream& in, int marks [], int arraySize) { ...
Then in main(), move that function call to firstArray down after the files are opened, and pass the parameters as shown above.
void main()
{
ifstream fin;
ofstream fout;
char IFname[20], OFname[20];
int marks [12];
char name[40];
while (!getInputFilename(IFname))
{
cout << "Invalid filename try again!\n\n";
}
while (!getOutputFilename(OFname))
{
cout << "Invalid filename try again!\n\n";
}
fout.open(OFname);
fin.open(IFname);
firstArray(fin,marks,12);
// fout << marks << endl;
for(int i = 0; i < 12; i++)
fout << marks[i] << endl;
fout.close();
fin.close();
}After opening the files you need to make a sanity check to see if the files were opened ok. Something like this
C++ Syntax (Toggle Plain Text)
fin.open(IFname); if(!fin.is_open()) { cout << "cannot open file " << IFname << endl; return 1; }
•
•
Join Date: Jan 2006
Posts: 12
Reputation:
Solved Threads: 0
Thanks very much mate, i made the changes and was able to get data from a text file into an array, and output it into another text file.
The test file i used i just contained 10 numbers, however the text file input data i want to use contains:
name //can be 3 words seperated by spaces
10 numbers // all ints
so eg:
Bla Bla Bla
13 18 38 49 58 69 94 85 48 95
Bleh Bleh Bleh
83 47 38 49 03 27 45 48 04 58
How would i change my code so it can output data in the form of:
name - average of first 5 numbers - average of last 5 numbers
name - average of first 5 numbers - average of last 5 numbers
The test file i used i just contained 10 numbers, however the text file input data i want to use contains:
name //can be 3 words seperated by spaces
10 numbers // all ints
so eg:
Bla Bla Bla
13 18 38 49 58 69 94 85 48 95
Bleh Bleh Bleh
83 47 38 49 03 27 45 48 04 58
How would i change my code so it can output data in the form of:
name - average of first 5 numbers - average of last 5 numbers
name - average of first 5 numbers - average of last 5 numbers
to this twice (or put in loop that runs until end-of-file
C++ Syntax (Toggle Plain Text)
fin >> name; firstArray(...);
•
•
Join Date: Jan 2006
Posts: 12
Reputation:
Solved Threads: 0
#include <iostream.h>
#include <string>
#include <fstream>
using namespace std;
bool getInputFilename(char fname[])
{
ifstream fin;
cout << "Please enter the filename for input : ";
cin >> fname;
fin.open(fname, ios::nocreate);
if (!fin.is_open())
return false;
fin.close();
return true;
}
bool getOutputFilename(char fname[])
{
ofstream fout;
cout << "Please enter the filename for output : ";
cin >> fname;
fout.open(fname);
if (fout.fail())
return false;
fout.close();
return true;
}
void firstArray(ifstream& fin, int marks [], int arraySize, char name[])
{
while (!fin.eof()) {
for (int i = 0; i < 12; i++) {
fin >> marks[i];
fin >> name;
}
}
}
void main()
{
ifstream fin;
ofstream fout;
char IFname[20], OFname[20];
int marks [12];
char name [3];
while (!getInputFilename(IFname))
{
cout << "Invalid filename try again!\n\n";
}
while (!getOutputFilename(OFname))
{
cout << "Invalid filename try again!\n\n";
}
fout.open(OFname);
fin.open(IFname);
firstArray(fin,marks,12, name);
while (!fin.eof()) {
for(int i = 0; i < 12; i++)
fout << marks[i] << endl;
fout << name << endl;
}
fout.close();
fin.close();
}Didnt quite understand you, attempted changes i made are in red, doesnt work but just trying to figure it out.
move fin >> name OUTSIDE that loop! there is only one instance of it on a line. that whole look is wrong anyway -- should not use eof() like that because it sometimes produces undesireable results.
C++ Syntax (Toggle Plain Text)
fin >> name; i = 0; while (i < 12 && fin>>marks[i]) { i++; }
•
•
Join Date: Jan 2006
Posts: 12
Reputation:
Solved Threads: 0
Ok now when i run the program it doesnt close and just keeps running for some reason after asking for the output file?
changed what you said to:
Really appreciate you taking the time to help.
changed what you said to:
C++ Syntax (Toggle Plain Text)
#include <iostream.h> #include <string> #include <fstream> using namespace std; bool getInputFilename(char fname[]) { ifstream fin; cout << "Please enter the filename for input : "; cin >> fname; fin.open(fname, ios::nocreate); if (!fin.is_open()) return false; fin.close(); return true; } bool getOutputFilename(char fname[]) { ofstream fout; cout << "Please enter the filename for output : "; cin >> fname; fout.open(fname); if (fout.fail()) return false; fout.close(); return true; } void firstArray(ifstream& fin, int marks [], int arraySize, char name[]) { while (!fin.eof()) { for (int i = 0; i < 12; i++) { fin >> marks[i]; fin >> name; } } } void main() { ifstream fin; ofstream fout; char IFname[20], OFname[20]; int marks [12], i; char name [3]; while (!getInputFilename(IFname)) { cout << "Invalid filename try again!\n\n"; } while (!getOutputFilename(OFname)) { cout << "Invalid filename try again!\n\n"; } fout.open(OFname); fin.open(IFname); firstArray(fin,marks,12, name); fin >> name; i = 0; while (i < 12 && fin>>marks[i]) { i++; } fout.close(); fin.close(); }
Really appreciate you taking the time to help.
![]() |
Similar Threads
- how to read data from text file into data grid??? (VB.NET)
- conversion of text file into data base file (Visual Basic 4 / 5 / 6)
- Reading an array from a file (Java)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C++ Forum
- Previous Thread: Can't get loop to work properly
- Next Thread: 'double' problem
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






