Hello everyone,
First of all, thanks in advance for your help.
I am trying to write a program that will examine my T-Mobile csv file. A csv file is a comma seperated values file which in my case contains the date, time, caller ID info, number, and duration of every call I've made in the past month, along with some other junk info. What I am trying to do is convert the file from that, into an output file that will show each number only once and the total number of minutes that I've spoken to each person.
This is a sample of the beginning of the csv file:
Code:
"LOCAL AIRTIME, LONG DISTANCE and INTERNATIONAL CHARGES"
Date,Destination,Time,Number,Call Type,Minutes,Airtime,Toll,Total
"5/28/10","Ptstlucies, FL","12:47 AM","772-777-7777","","1","-","-","-"
"5/28/10","Pueblo, CO","12:50 AM","719-588-8888","","1","-","-","-"
"5/28/10","Stuart, FL","12:51 AM","772-999-9999","","3","-","-","-"
This, is the code that I have written:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFSIZE 100
char *strsh(char *lstr, int charnum);
char *strsh(char *lstr, int charnum){ //This function will shift the string "lstr" to the left charnum characters
int i = 0; // E.g. The string "This is a string" feed to the function with charnum equal to 5
// will make the string become "is a string"
while ( charnum <= strlen(lstr)){
lstr[i] = lstr[charnum];
i++;
charnum++;
}
//printf("%s", lstr);
return lstr;
}
int main(int argc, char *argv[])
{
char buf[ BUFSIZE ] = {0}; //This is the buffer into which data from the file is read
char buf2[ BUFSIZE ] = {0}; //This is the backup buffer which is where buf is copied when it is split with strtok
int counter;
int counter2;
int tempcount;
int bufcount;
char * pch; //This is what the tokens from buf are stored into with strtok.
int arraySize = 200;
//char *NumArray[50] = (char *)malloc(sizeof(char *)); // This is the old declaration of NumArray
char **NumArray = (char **)malloc(arraySize * sizeof(char *)); //Attempting to make an array of pointers to store the phone numbers.
int n = 0;
int m = 0;
int q = 0;
//int azer = 0;
//NumArray[0] = "Testing";
//printf("\n\n %s \n\n", NumArray[0]);
//for(azer = 0; azer < 50; azer++){
// strcpy(NumArray[azer], NULL);
// }
FILE *in = fopen("calls.csv", "r"); //Open the csv file with the phone numbers and times
if (in == NULL) {
perror("Unable to open file\n");
exit(EXIT_FAILURE);
}
FILE *out = fopen("output2.txt", "w"); //Open the output file into which we will put the parsed data
if (in == NULL) {
perror("Unable to open file\n");
exit(EXIT_FAILURE);
}
for( counter = 0; fgets( buf, BUFSIZE, in); counter++ ){ //Keep looping until we've read the entire file with fgets()
if(counter >= 3){ // Skip the first 3 lines because those don't contain phone data
strsh(buf, 11); // Get rid of the date and time of the call and other junk data
tempcount = 0;
bufcount = 0;
while(tempcount < 3){ //While loop to remove more junk from the begining of the file, since
if(buf[bufcount] == '\"') //The next piece of data varies in size, we have to count how many quotes
tempcount++; //There are up to the end of the junk data and then remove however many characters
bufcount++; //in the third quote is.
}
strsh(buf, (bufcount + 1));
tempcount = 0;
bufcount = 0;
while(tempcount < 6){ //Find out how far it is to the end of the data we need so that we can
if(buf[bufcount] == '\"') //Cut out the rest of the line with a \0
tempcount++;
bufcount++;
}
buf[bufcount] = '\n';
buf[(bufcount + 1)] = '\0'; //Cutting off junk at end of line (See above)
//printf("\n\ntempcount = %d bufcount = %d \n\n", tempcount, bufcount);
//system("pause");
for(counter2 = 0; counter2 < sizeof(buf); counter2++ ){ //This loop removes the quotes from what we have remaining in the line
if(buf[counter2] == '\"')
buf[counter2] = ' ';
}
if(buf[1] != ','){ //This will remove the last line of the file which contains no useful data and begins with a comma
strcpy(buf2, buf); //Backing up buf before we use strtok to break it up
pch = strtok(buf, ","); //Split buf at the commas
m = 1;/*
for(n = 0; n < 50; n++){ //This loop, which does not work is suppoed to check each number against everything
printf("Debug1\n"); //in the array to determine if we've already stored the number but for some reason this
if(strcmp(pch, NumArray[n]) == 0){ //keeps causing a segmentation fault
printf("Debug2\n");
printf("pch is equal to NumArray \n");
m = 0;
}
}
printf("m = %d \n", m); //This is debugging stuff if m == 1 then it's supposed to
if(m == 1){ //store pch (the number from buf)into NumArray.
NumArray[q] = pch;
q++;
printf("Stored %d \n", q);
}*/
system("pause"); //Pause to see the results of processing on this line
puts(pch); //Display data to the screen
fprintf(out, "%s", buf); //Write data to file
}
}
}
fclose(in); //Close the csv file
fclose(out); //Close the output file.
system("pause"); //Pause one more time for no particular reason.
return 0; //Return 0 to function main()
}
Once buf gets to the loop causing the segmentation fault, it looks like this:
772-777-7777 , , 1
and pch splits it at the comma, so what's being compared with NumArray[n] is
772-777-7777
Now I'm having trouble with 2, or maybe 3 parts. First of all, I need to declare an array of pointers (to strings). This array of pointers will hold the phone numbers that I extract from the file. I tried declaring it like this:
char *NumArray[50];
But the program crashed when I got to checking the contents of the array, so I thought that maybe I had to malloc space for the array (which is my second question), so I commented that out, and tried this:
char **NumArray = (char **)malloc(arraySize * sizeof(char *));
But I'm still crashing, and I don't even quite know that this is doing the same thing that I was doing before. So my first question is what is the best way, or the proper way, to declare an array of pointers (to strings)
My second question is, do I need to, and if so how do I malloc space for the array properly?
Last, how can I properly make the program check each element of the array against "pch" which is the phone number extracted from "buf". Currently I have this loop, which crashes the app:
for(n = 0; n < 50; n++){
printf("Debug1\n");
if(strcmp(pch, NumArray[n]) == 0){
printf("Debug2\n");
printf("pch is equal to NumArray \n");
m = 0;
}
}
Again, thanks in advance for your help.
-Primux