Hi,
Im trying to do a project in C but I have no previous experience. I was wondering how easy it would be to read and tabulate the contents of a CSV file from excel.
Are there any example scripts on the internet designed to do this? Would anyone be able to give me some insight on how it could be achieved?
Ive done some research on google and really havent got anywhere.
James
Reading CSV contents
Started by spadez, Feb 02 2009 09:16 AM
9 replies to this topic
#1
Posted 02 February 2009 - 09:16 AM
|
|
|
#2
Posted 02 February 2009 - 09:22 AM
The first thing you need to do is to look at how CSV files are made.
With a CSV file, every row of the file is a new row in the table. Every cell in the row is separated, or delimited, with a comma. Example:
See? Then you only need to read in the file and look for all the commas, to parse out the CSV file.
With a CSV file, every row of the file is a new row in the table. Every cell in the row is separated, or delimited, with a comma. Example:
Cell 1,Cell 2,Cell 3,Cell 4 Cell 1,Cell 2,,Cell4 ,,,Cell4 ,,Cell3,
See? Then you only need to read in the file and look for all the commas, to parse out the CSV file.
#3
Posted 02 February 2009 - 09:50 AM
Hi Xav.
Thank you for the information, that sounds a lot more simple than I thought it would be. Now I need to work out how to read the file.
I dont know anything about C at the moment and my google searches havent turned up anything I can use. I dont suppose you know a tutorial online that explains how to read this text information with c?
EDIT: 1+ rep for the help!
Thank you for the information, that sounds a lot more simple than I thought it would be. Now I need to work out how to read the file.
I dont know anything about C at the moment and my google searches havent turned up anything I can use. I dont suppose you know a tutorial online that explains how to read this text information with c?
EDIT: 1+ rep for the help!
#4
Posted 02 February 2009 - 10:01 AM
Thanks, but you have a Rep Power of 0, which means your rep has no effect. :)
How about something like this:
How about something like this:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main () {
string line; //this will contain the data read from the file
ifstream myfile ("example.csv"); //opening the file.
if (myfile.is_open()) //if the file is open
{
while (! myfile.eof() ) //while the end of file is NOT reached
{
getline (myfile,line); //get one line from the file
//DO THE CSV PARSING HERE.
//FOR NOW I WILL JUST OUTPUT THE LINE:
cout << line << endl; //and output it
}
myfile.close(); //closing the file
}
else cout << "Unable to open file"; //if the file is not open output <--
return 0;
}
#5
Posted 02 February 2009 - 01:09 PM
Thank you again for the help, this gives me a good starting point to work from.
Im sorry my rep has no effect :(
Im sorry my rep has no effect :(
#6
Posted 02 February 2009 - 04:40 PM
Hi,
I was wondering if I could take this a step further. Im trying to make a window form window which will contain my code. On the front page of the program I want a user to "browse" for their CSV file, and then use this as the variable for the file location within the program.
In the first instance, how am I meant to add a browse button?
Toolbox > Dialoug > Openfile Dialoug
Is this what I need to put a file browse button on my program?
I was wondering if I could take this a step further. Im trying to make a window form window which will contain my code. On the front page of the program I want a user to "browse" for their CSV file, and then use this as the variable for the file location within the program.
In the first instance, how am I meant to add a browse button?
Toolbox > Dialoug > Openfile Dialoug
Is this what I need to put a file browse button on my program?
#7
Posted 03 February 2009 - 09:44 AM
Also it has just become apparent that the code is C++ and I need this program to be C. Is there a way this could be coded in C?
#8
Posted 03 February 2009 - 09:47 AM
As to the browsing, an OpenFileDialog would do the job perfectly.
Take a look here: C Tutorial File Handling Commands: How to Open, Close, Read, Write, Append and Delete Files in C Code
Take a look here: C Tutorial File Handling Commands: How to Open, Close, Read, Write, Append and Delete Files in C Code
#9
Posted 03 February 2009 - 10:13 AM
Is this file a good example of how a C program can read a text file?
/*
Program#file2.c
Example of parameter passing using argc and argv
*/
/*Search specified file for specified character. */
#include <stdio.h>
#include <stdlib.h>
void main(int argc, char *argv[])
{
FILE *fp; /* file pointer */
char ch;
/* see if correct number of command line arguments */
if(argc !=3)
{
printf("Usage: find <filename> <ch>\n");
exit(1);
}
/* open file for input */
if ((fp = fopen(argv[1], "r"))==NULL)
{
printf("Cannot open file \n");
exit(1);
}
/* look for character */
while ((ch = getc(fp)) !=EOF) /* where getc() is a */
if (ch== *argv[2])
{ /*function to get one char*/
printf("%c found",ch); /* from the file */
break;
}
fclose(fp);
}
#10
Posted 03 February 2009 - 10:44 AM


Sign In
Create Account


Back to top









