Hello all,
I have what at first glance seems like a very simple problem but I have been getting bogged down with it for the last few days now and would appreciate some help.
I have a series of text files to read in, and wish to convert them to binary files to reduce the file size. The text files contain data arranged in multiple columns and rows - a regular 2 D array. The first row contains text for the column headers, the rest are numbers which could be represented by floats.
Unfortunately the files are space delimited and the number of spaces varies between the entries for each of the columns. Things are further complicated by the fact that the separation is not even consistent between rows either.
My "plan" was to read the file and extract each element and put it into a character array so that I could at least have everything properly organised before sorting out my numbers and converting the whole thing to a binary file.
However I have struggled to get past the first point in this plan. Using some of the previous threads I have initiated my target char arr Table
and I have been able to remove the white space from the file, such that the program pulls out each useful bit of text as a char called data
, but have lost all the new-line information, and I am not sure how I am going to assign data
to an element in my char array. It has been some 5 years since I last did C++ and I am somewhat rusty. My code is below and I have attached an example text file to read in. Any help would be gratefully recieved.
#include <stdlib.h>
#include <stdio.h>
#include <iostream
#include <fstream>
#include <string>
using namespace std;
int main ()
{
//------------------------------------------------------------------------------------
// DECLARATIONS
typedef unsigned short int us_int;
us_int x; //an integer counter
us_int y; //an integer counter
const us_int cols=5; //the total columns in the array
const us_int rows=3; //the rows in the array
char FileName[80];
string data;
char Table[cols][32]; //char array to hold the data from the file - 31 bits for each element
char ***TableP; //pointer for the Table
TableP = (char ***)malloc(rows * (sizeof *TableP));
//------------------------------------------------------------------------------------
// STAGE 1: ACQUIRE THE DATA.
// This stage of the program reads in the text file.
// Read the file containing the spaced columns of data. Each entry is treated as a character (non-numeric)
// with the spaces being used to denote separate entities in the array (different numbers)
//cout << "\nPlease enter the name of your data file, including file extension: ";
//cin >> FileName;
cout << "\nThank you\nReading the raw data file...\n";
//ifstream ReadInFile (FileName);
ifstream ReadInFile ("s.txt");
ofstream WriteOutFile ("readout.txt");
x = 0;
if (ReadInFile.is_open())
{
while(ReadInFile >> data)
{
x++;
cout << data << "\t";
WriteOutFile << data << "\t";
//cout << x << ": ";
}
ReadInFile.close();
WriteOutFile.close();
}
else cout << "Unable to open file";
cout << "\n";
cout << "\nPress any key and enter to exit!";
cin >> y;
cout << "\nDone\n";
//------------------------------------------------------------------------------------
return 0;
}
just found out I couldn't attach the file so please copy this (from within the inverted commas) and save it as s.txt
" This Is The First Line
This Is The Second Line
1.2343 5.565 1.5485 0.0096 0.76854"