0

How to read in a text file with commas and write it out to a new file without commas?

The file has nine columns- 1st is month next is date and there is a comma, then year then there are 6 columns of numbers representing dollars with a comma in between them for example 17,751.24

I need to make a new text file out of it that has no commas.

I was trying to use double variables for each coloumn but the commas forced me to use string variables then i tried to read it in with a while loop:

While(infile >> month >> date >> year >> open >> high >> low >> close >> volume >> adj) 
{ 
... 
}

Side note : the file is about some stock exchange thing.

How do I make a new text file with the same content except there should be no commas.

This is for a beginners c++ class so please avoid using advanced stuff in answers.

Thanks in advance.

Love Patel
  • 19
  • 6
  • 1
    The obvious would be to just create some variables named something like "comma" or "ignore", and read the commas into them. Then write out the fields you care about. – Jerry Coffin Mar 17 '15 at 22:15
  • That would not work as commas are together between the numbers without a space, If I want to read the commas in a new variable then I would have to put space before and after each comma in the file which I can't. – Love Patel Mar 17 '15 at 22:17
  • 1
    `This is for a beginners c++ class so please avoid using advanced stuff in answers.` You aren't asking us to help you cheat, are you? – djs Mar 17 '15 at 22:24
  • 3
    There are literally hundreds of existing questions regarding reading text files in C++. Surely a search would find one about reading comma-separated value (CSV) files to help you get started, I'd think. Let me get you started with a search for `[c++] read csv file`: http://stackoverflow.com/search?q=[c%2B%2B]+read+csv+file – Ken White Mar 17 '15 at 22:25
  • 1
    http://stackoverflow.com/questions/10376199/how-can-i-use-non-default-delimiters-when-reading-a-text-file-with-stdfstream is how to swap delimiters. – Yakk - Adam Nevraumont Mar 17 '15 at 22:26
  • @LovePatel: Not so--just make a variable of type `char` instead of (for example) string. That will skip white space (if any) then read one non-white-space character. The tricky part will be reading the numbers--if you try to read a number where there is none, that will set the stream to the failed state, so you'll have to try to read, then reset, read a comma, and proceed to the next field. – Jerry Coffin Mar 17 '15 at 22:32
  • @JerryCoffin Any variable skips the white space. The problem is you cant read in a single comma which is not separated by white spaces. – Love Patel Mar 17 '15 at 22:34
  • @djs No the project is much bigger than that. I am only asking a small part of it. – Love Patel Mar 17 '15 at 22:36
  • Yes, you can. Read into a `char`. – Jerry Coffin Mar 17 '15 at 22:36
  • @JerryCoffin But that will require SOOOOOO MANYYY char variables !!! – Love Patel Mar 17 '15 at 22:37
  • @LovePatel: As long as you don't care about checking the contents, you only need *one* char variable. Just read into the same one repeatedly. – Jerry Coffin Mar 17 '15 at 22:40
  • @JerryCoffin I need to use the last coloumn and sort it from low to high and store it into a 50 element array and then print out the max and min value on screen – Love Patel Mar 17 '15 at 22:43
  • @KenWhite I searched it all and they are all using some advanced stuff like class and what not I need a simple solution for a beginners level – Love Patel Mar 17 '15 at 22:48
  • There are certainly some that use *advanced stuff*, but there are dozens and dozens more that do not. I highly doubt you "searched it all". – Ken White Mar 17 '15 at 22:52
  • @LovePatel: I'm just talking about a char variable to read commas into. Yes, you'll want `int` or `double` (or whatever) to hold the numbers, but you're going to need those regardless. – Jerry Coffin Mar 17 '15 at 22:58
  • @KenWhite I did read most of it and Couldn't get to understand it. Also They are all trying to seperate the values when they encounter comma, Which were are not supposed to do here, If the value is 17,898.50 then We cant use two seperate values called 17 and 898.50 instead here we need to ignore the comma so it reads like 17898.50 !! – Love Patel Mar 17 '15 at 22:59
  • @JerryCoffin How do I use the char variable to read commas when I need to use the whole number in double for example: If the number is 17,898.50 How Would I read ',' into char and 17898.50 into double????? – Love Patel Mar 17 '15 at 23:03
  • @LovePatel: For that, you have little choice but to do something at least slightly complicated. The easiest way is to change to a locale that knows about thousands separators in numbers. Next easier is a small state machine that knows how to read an entire number, including commas (if any). Simple fact is, you're defining a task that's well beyond what you should be throwing at beginners though. – Jerry Coffin Mar 17 '15 at 23:06
  • @JerryCoffin I think it has something to do with Arrays!?? Coz thats what we are learning in class right now. – Love Patel Mar 17 '15 at 23:08

1 Answers1

0

Perhaps this is still too soon, but as what I provide below is not a complete answer, I hope it might help to get you going.

The simple question seems to be:

How to read in a text file with commas and write it out to a new file without commas?

This simple question is broad (with many approaches), but you might consider the following:

// To use:
//     invoke with i/o redirection
//
//     Example with executable named t160
//     and some file named 'inputFile':
//
//     t160  <  inputFile  > noCommaFile
//
int t160()
{
   int commaCount = 0;

   do {

      int kar = 0;

      kar = std::cin.get();           // read single char from cin

      if(std::cin.eof()) { break; }   // break out when we finish

      if(std::cin.bad()) {            // break out on file i/o error
         std::cerr << "ERR: std::cin.bad() " << std::endl;
         break;
      }

      if(',' == kar) { commaCount += 1; continue; } // found a comma
      //                                ^^^^^^^^ continue loop: do not cout the comma

      std::cout << char(kar);         // not a comma, so cout the kar

   }while(1);

   // uncomment for test:
   // std::cerr << "commaCount = " << commaCount << std::endl;

   return(0);
}

This code reads one char at a time, and writes one char at a time or nothing.

When a comma is found, it is 'discarded'. All other kars are included in the output.

You might later decide that you want to replace the comma with a space (instead of discarding it).

I can imagine that you might decide to treat a comma-space (", ") differently than a comma-digit (as in "123,456").

This simple loop should support those decisions, and many more.

Good luck.

2785528
  • 5,162
  • 2
  • 16
  • 18