0

I'm trying to create a code to read from a file and count how many iterations the aforementioned word is in the .csv file then output it to the screen. I'm using Xcode and keep getting the (11db) error. Someone please Help!

Short .csv file extract

28/02/17 07:25  b"I'm done goodnight"       
28/02/17 07:14  b'If I could loop the sound of my tiny dog drinking water so delicately it would make me this emoji: \xe2\x98\xba\xef\xb8\x8f'      
28/02/17 02:33  b'About last night...\xe2\x9c\xa8\xf0\x9f\x8d\xab @jpgaultier @vanityfair   
27/02/17 20:01  b'Last night was wild! Even I woke up with an #Oscar! (harharharhar)    
27/02/17 05:51  b"You're a winner KP    
27/02/17 05:12  b'WHAT WAIT OMG'        
27/02/17 05:12  b'KATY HERE. I lost a bet for best picture (I wanted moonlight...'      
27/02/17 05:10  b'I just want to say VICE is the best thing ever and Shane smith has a fine head of hair.'      
27/02/17 03:37  b'RT @RheaButcher: Ever. In 2017.       
26/02/17 23:19  b'GET OUT and see "Get Out" ASAP! Love my girl aw in it     

Code

#include <stdio.h>
#include <cstring>
#include <iostream>
#include <fstream>
using namespace std;

int main()
{

ifstream fin("/Users/williamthatcher/Desktop/TwitterFeed/sampleTweets.csv"); //opening text file

int count=0;
char ch[20],c[20];

cout << "Enter any word you want to count:";
cin>>c;

while(fin)
{
    fin>>ch;
    if(strcmp(ch,c)==0)
        count++;
}

cout<<"Occurrence of word" << c << " = " << count << endl;
fin.close(); //closing file

return 0;

}
CapriSun7
  • 1
  • 1
  • Where is there reading of CSV fields? – Vlad from Moscow Jun 01 '17 at 14:47
  • First line in the main section, thats the directory for the .csv file – CapriSun7 Jun 01 '17 at 14:49
  • @VladfromMoscow they just need to count words. The fact that it's a csv file seems to be irrelevant. – Federico klez Culloca Jun 01 '17 at 14:49
  • You are correct, can you help me? – CapriSun7 Jun 01 '17 at 14:51
  • What's error 11db? When does it happen? – Federico klez Culloca Jun 01 '17 at 14:53
  • Not to sure what the error is, but it does says Thread 1: EXC_BAD_ACCESS (code=EXC_I386_GPFLT) It happens after i type in a word to search and press enter – CapriSun7 Jun 01 '17 at 14:57
  • What do you think happens if either `c` or `ch` is longer than 20 characters? I don't know what's in that csv file, but I can see it happening. – Federico klez Culloca Jun 01 '17 at 14:59
  • I have found this page talking about the error, but it doesn't specify what exactly is causing it or how to fix https://stackoverflow.com/questions/19651788/whats-the-meaning-of-exception-code-exc-i386-gpflt – CapriSun7 Jun 01 '17 at 14:59
  • I imagine that could be whats breaking it as it is a large file full of twitter ****. However i just tried changing their values to 160 each and the error still occurs :( – CapriSun7 Jun 01 '17 at 15:01
  • Does this code work for you if you change the file directory? – CapriSun7 Jun 01 '17 at 15:21
  • How should I know? I don't know what's in the file. Can you please include the first couple of rows in your question? – Federico klez Culloca Jun 01 '17 at 15:23
  • Done, also i just created a small .txt file to test against that, the program ran fine, however capitalisation problems were misleading. So also would you know what to add to the code to make it include the capitalisation version of the word entered as well? – CapriSun7 Jun 01 '17 at 15:33
  • I tried this on a linux machine, it just gives a very wrong answer but doesn't crash. If I were you I'd use `string` instead of `char[]` and the equal operator instead of `strcmp`. The less C-isms you have in your C++ code, the better. About case-insensitive comparison, I don't know of a standard library way to do that. – Federico klez Culloca Jun 01 '17 at 15:38
  • I changed it to this, the program runs but doesn't feedback the correct answers. int count=0; string number; string word; cout << "Enter any word you want to count:"; cin>>word; while(fin) { fin>>number; if(word == number) count++; } cout<<"Occurrence of the word: " << word << " = " << count << endl; fin.close(); //closing file – CapriSun7 Jun 01 '17 at 16:02
  • I suggest you pick a debugger and step through your code to try and understand what goes wrong. – Federico klez Culloca Jun 01 '17 at 20:54

0 Answers0