Jump to content

Reading CSV contents

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
9 replies to this topic

#1
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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

#2
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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:

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.
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#3
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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!

#4
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
Thanks, but you have a Rep Power of 0, which means your rep has no effect. :)

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;
}

Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#5
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
Thank you again for the help, this gives me a good starting point to work from.

Im sorry my rep has no effect :(

#6
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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?

#7
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
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
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums

#9
spadez

spadez

    Learning Programmer

  • Members
  • PipPipPip
  • 38 posts
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
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts
If it works, then use it. :) I am afraid I have never learned C or C++, so I can't give you an expert opinion. But as a general rule, if something works, then keep it. :)
Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums