| | |
CSV file
![]() |
•
•
Join Date: Nov 2005
Posts: 29
Reputation:
Solved Threads: 0
Hi
I am having a file called Log.csv which contains 196 columns 9 of them empty and 1447 rows, I start reading the file but as I think I am facing a problem of getting the whole data, actually it gives me empty screen when print out on the screen.
I need some help in reading the csv file and put it in array because later on I have to get the date (from the first column) and min,max for some other columns.
I am really gratefull if anyone can help me.
I am going to paste 1 line from my Log.csv and then paste my code.
11/10/2004,00:00:45,190,0,13.94,346.81,201.11,18.46,32,3,47,6, ,127,230,228,228,348,128,64,64,8,6,6,50,50,50,34,34,35,6,6,6,0, ,63,230,228,230,344,66,66,66,8,6,7,50,50,50,35,36,35,6,6,6,0, ,49.41,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,0.06,30.91,00,63,6,4,0, ,49.67,0.00,0.06,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,32.87,00,63,6,4,0, ,49.67,0.00,-0.30,32.87,00,63,6,4,0, ,49.94,0.00,-0.30,30.91,00,63,6,4,0, ,49.41,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,30.91,00,63,6,4,0, ,49.41,0.00,-0.30,32.87,00,63,6,4,0, ,49.67,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,32.87,00,63,6,4,0, ,49.41,0.00,0.06,32.87,00,63,6,4,0, ,49.67,0.00,0.06,30.91,00,63,6,4,0, ,
My code is
#include <iostream>
#include <fstream>
using namespace std;
float matrix_points[1447][196];
int main()
{
std::ifstream input_file("Log.csv");
int row(0), col(0);
char buffer[256];
char line[255];
input_file.clear();
input_file.seekg(0);
/* brings all the data in .csv file and put them in an array*/
while(!(input_file.eof()))
{
for ( col=0; col<196; col++)
{
if (matrix_points[row][col] != matrix_points[row][195])
{
input_file.getline(line, sizeof(buffer), ',');
matrix_points[row][col] = atof(line);
}
else
{
input_file.getline(line, sizeof(buffer), '\n');
matrix_points[row][12] = atof(line);
}
}
row++;
}
/* for loop to print data on screen*/
for (int i=0; i< row ; i++)
for (int j=0; j< col ; j++)
{
if (matrix_points[i][j] != matrix_points[i][195])
cout << matrix_points[i][j] << " " ;
else
cout << matrix_points[i][195]<< endl;
}
cout << row <<endl;
cout << "\n\n";
// system("PAUSE");
return 0;
}
I am having a file called Log.csv which contains 196 columns 9 of them empty and 1447 rows, I start reading the file but as I think I am facing a problem of getting the whole data, actually it gives me empty screen when print out on the screen.
I need some help in reading the csv file and put it in array because later on I have to get the date (from the first column) and min,max for some other columns.
I am really gratefull if anyone can help me.
I am going to paste 1 line from my Log.csv and then paste my code.
11/10/2004,00:00:45,190,0,13.94,346.81,201.11,18.46,32,3,47,6, ,127,230,228,228,348,128,64,64,8,6,6,50,50,50,34,34,35,6,6,6,0, ,63,230,228,230,344,66,66,66,8,6,7,50,50,50,35,36,35,6,6,6,0, ,49.41,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,0.06,30.91,00,63,6,4,0, ,49.67,0.00,0.06,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,32.87,00,63,6,4,0, ,49.67,0.00,-0.30,32.87,00,63,6,4,0, ,49.94,0.00,-0.30,30.91,00,63,6,4,0, ,49.41,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,30.91,00,63,6,4,0, ,49.41,0.00,-0.30,32.87,00,63,6,4,0, ,49.67,0.00,-0.30,30.91,00,63,6,4,0, ,49.67,0.00,-0.30,32.87,00,63,6,4,0, ,49.41,0.00,0.06,32.87,00,63,6,4,0, ,49.67,0.00,0.06,30.91,00,63,6,4,0, ,
My code is
#include <iostream>
#include <fstream>
using namespace std;
float matrix_points[1447][196];
int main()
{
std::ifstream input_file("Log.csv");
int row(0), col(0);
char buffer[256];
char line[255];
input_file.clear();
input_file.seekg(0);
/* brings all the data in .csv file and put them in an array*/
while(!(input_file.eof()))
{
for ( col=0; col<196; col++)
{
if (matrix_points[row][col] != matrix_points[row][195])
{
input_file.getline(line, sizeof(buffer), ',');
matrix_points[row][col] = atof(line);
}
else
{
input_file.getline(line, sizeof(buffer), '\n');
matrix_points[row][12] = atof(line);
}
}
row++;
}
/* for loop to print data on screen*/
for (int i=0; i< row ; i++)
for (int j=0; j< col ; j++)
{
if (matrix_points[i][j] != matrix_points[i][195])
cout << matrix_points[i][j] << " " ;
else
cout << matrix_points[i][195]<< endl;
}
cout << row <<endl;
cout << "\n\n";
// system("PAUSE");
return 0;
}
•
•
Join Date: Jul 2005
Posts: 1,673
Reputation:
Solved Threads: 261
As a new member to the board it will behoove you to read the instructions on how to post code to maintain the indentation protocol you wrote the code with. To do that, enclose any posted code in code tags, that is [ code ] before the code and [ /code] after the code (with out the space between the square brackets and the contents between the brackets.
As a new programmer, I encourage you to try to do things modularly. In this case I'd try to read in just the first item from the file. When I could do that, I'd try reading in the first line of the file. When I could do that, I'd try reading in the entire file.
When you get to the stage where you are reading the whole file, note that using the return of eof() as a conditional in a control statement is not the best idea---it frequently leads to undefined behaviour such as inserting the last data read in twice. You can search the board for excruciating detail if you want.
Instead, since each line has exactly 196 fields and there are exactly 1447 lines, I'd use nested for loops rather than a while loop/for loop combination use input_file.getline(line, 256, ','); as the conditional (why bother calling sizeof() each time??). Then within the loop I'd do something like this:
Now if I didn't know there were exactly 1447 lines, then a conditional loop would be reasonable.
In addition I'm not sure what atof() will do with a string that consists of all whitespace, so I would develop a protocol to identify a "blank" column, and not call atof() if line consisted of a single space.
As a new programmer, I encourage you to try to do things modularly. In this case I'd try to read in just the first item from the file. When I could do that, I'd try reading in the first line of the file. When I could do that, I'd try reading in the entire file.
When you get to the stage where you are reading the whole file, note that using the return of eof() as a conditional in a control statement is not the best idea---it frequently leads to undefined behaviour such as inserting the last data read in twice. You can search the board for excruciating detail if you want.
Instead, since each line has exactly 196 fields and there are exactly 1447 lines, I'd use nested for loops rather than a while loop/for loop combination use input_file.getline(line, 256, ','); as the conditional (why bother calling sizeof() each time??). Then within the loop I'd do something like this:
C Syntax (Toggle Plain Text)
for(row... for(col... input_file.getline(line, 256, ','); //(why bother calling sizeof() each time??) matrix_points[row][col] = atof(line);
C Syntax (Toggle Plain Text)
row = -1; while (input_file.getline(line, 256, ',')) //if this works there are 195 more to go on this line matrix_points[++row][0] = atof(line);//start a new row and //place the sentinel value at col == 0; for(col = 1; ... //the rest go from 1 to < 196 matrix_points[row][col] = atof(line);
In addition I'm not sure what atof() will do with a string that consists of all whitespace, so I would develop a protocol to identify a "blank" column, and not call atof() if line consisted of a single space.
•
•
Join Date: Nov 2005
Posts: 29
Reputation:
Solved Threads: 0
Thank you Lerner for your reply, I did many things starting with reading the first item and the I cut a bit of the Log.csv file and it was working perfect with only 10 column and 1447 row, and it enabled me to get the min and max for one of the column.
but all the problem started when I start increasing the numbers of the column I want to read, yesterday code start only giving me a one vertical line of 0s. this why I seek anyone help. I attach the Log.csv in a zip file
C Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <fstream> using namespace std; float matrix_points[10000][10]; int main() { std::ifstream input_file("test.csv"); int counter(0); char buffer[256]; char line[255]; input_file.clear(); input_file.seekg(0); while(!(input_file.eof())) { input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][0] = atoi(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][1] = atoi(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][2] = atof(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][3] = atof(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][4] = atof(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][5] = atof(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][6] = atoi(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][7] = atoi(line); input_file.getline(line, sizeof(buffer), ','); matrix_points[counter][8] = atoi(line); input_file.getline(line, sizeof(buffer), '\n'); matrix_points[counter][9] = atoi(line); counter++; } for (int i=0; i<counter; i++){ cout << matrix_points[i][0] << " " << matrix_points[i][1] << " " << matrix_points[i][2] << " " << matrix_points[i][3] << " " << matrix_points[i][4] << " " << matrix_points[i][5] << " " << matrix_points[i][6] << " " << matrix_points[i][7] << " " << matrix_points[i][8] << " " << matrix_points[i][9] << endl; } cout << counter<<endl; cout << "\n\n"; // system("PAUSE"); return 0; }
but all the problem started when I start increasing the numbers of the column I want to read, yesterday code start only giving me a one vertical line of 0s. this why I seek anyone help. I attach the Log.csv in a zip file
•
•
Join Date: May 2004
Posts: 178
Reputation:
Solved Threads: 10
This is a simple-minded C version. You can use strtok in C++ as well.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXFLDS 200 /* maximum possible number of fields */ #define MAXFLDSIZE 32 /* longest possible field + 1 = 31 byte field */ void parse( char *record, char *delim, char arr[][MAXFLDSIZE],int *fldcnt) { char*p=strtok(record,delim); int fld=0; while(*p) { strcpy(arr[fld],p); fld++; p=strtok('\0',delim); } *fldcnt=fld; } int main(int argc, char *argv[]) { char tmp[1024]={0x0}; int fldcnt=0; char arr[MAXFLDS][MAXFLDSIZE]={0x0}; int recordcnt=0; FILE *in=fopen(argv[1],"r"); /* open file on command line */ if(in==NULL) { perror("File open error"); exit(EXIT_FAILURE); } while(fgets(tmp,sizeof(tmp),in)!=0) /* read a record */ { int i=0; recordcnt++; printf("Record number: %d\n",recordcnt); parse(tmp,",",arr,&fldcnt); /* whack record into fields */ for(i=0;i<fldcnt;i++) { /* print each field */ printf("\tField number: %3d==%s\n",i,arr[i]); } } fclose(in); return 0; }
here is a c++ version that puts the strings into a 2d vector
C Syntax (Toggle Plain Text)
#pragma warning(disable: 4786) // VC++ 6.0 disable warning about debug line too long #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; typedef vector<string> LINE; int main() { string line; int pos; vector<LINE> array; ifstream in("log.csv"); if(!in.is_open()) { cout << "Failed to open file" << endl; return 1; } while( getline(in,line) ) { LINE ln; while( (pos = line.find(',')) >= 0) { string field = line.substr(0,pos); line = line.substr(pos+1); ln.push_back(field); } array.push_back(ln); } return 0; }
•
•
Join Date: Nov 2005
Posts: 29
Reputation:
Solved Threads: 0
:o
•
•
•
•
Hi again
I know you do not have your krystal ball with you, but if you read my first email you would know that I would like to get the date from the first column and min and max for other columns, can you give me some of your time and give me some hint on your code tomake it easier for me.
vectors are similar to normal arrays that relieve you with all the hassel and mistakes of memory allocations. The 2d vector is a vector of vectors. The inner-most vector contains the strings for each line, and the outer vector is the array of each line. So to get column 0 of row 0
The first row (from your data file) contains column names, so the above will get the name in column 0. There are other tricks to get a column based on its name instead of a column number -- if you ever add or subtract columns in the data file, using column names instead of numbers makes your program more portable, but slightly slower to process.
C Syntax (Toggle Plain Text)
vector<LINE> array; // get row 0 LINE& row = array[0]; // get column 0 string& s = row[0];
The first row (from your data file) contains column names, so the above will get the name in column 0. There are other tricks to get a column based on its name instead of a column number -- if you ever add or subtract columns in the data file, using column names instead of numbers makes your program more portable, but slightly slower to process.
C Syntax (Toggle Plain Text)
string GetColData(int row_number,string ColName) { LINE& columnNames = array[0]; // find the collumn with the above column name for(int col = 0; col < columnNames.size(); col++) { if( ColName == ColumnNames[i] ) { // get data for this column LINE& row = array[row_number]; return row[col]; } return ""; }
![]() |
Similar Threads
- Reading in a *.csv file and loading the data into an Array (Java)
- Remove quotation marks from a CSV file (C#)
- Need Help Reading a csv file created from MSExcel (C)
- parsing csv file (PHP)
Other Threads in the C Forum
- Previous Thread: What am I doing wrong?
- Next Thread: Number System:Conversion from binary to decimal
| Thread Tools | Search this Thread |
* adobe ansi api array arrays binarysearch calculate centimeter changingto char character cm convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o inches intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h windowsapi






make it easier for me.