Array or Pointer (The Better Way?)
Okay, For a time now I have been working on a file sorter of sorts that
Takes a large file (approx 3-20meg) of text. The Prog finds the phone number and makes a file out of that, then the program checks for duplicate phone #'s and makes a new file of the contacts info. Program below:
Code:
#include "stdafx.h"
#include "stdlib.h"
#include "iostream.h"
#include "fstream.h"
#include "string.h"
#include <io.h>
#include <string>
#include <stdio.h>
#include <iomanip.h>
#include <strstrea.h>
char phonenumber[9];
int j=0;
char phonenumber2[9];
const int lineSize = 501;
int i=0;
int phonelist(const char *phonefilename,const char *filename)
{
char dataLine[lineSize];
ofstream fout2;
ifstream filename2(filename, ios::in);
fout2.open(phonefilename,fstream::in | fstream::out);//|fstream::app);
i=0;
while (filename2.getline(dataLine, lineSize, '\n'))
{
char *phonestr=0;
phonestr = strtok (dataLine+258," ");//finds the phone number
if ((i%2)==0)//for some reason it stays on the same line twice
fout2<<phonestr<<endl;
i++;
}
fout2.close();
return 0;
}
int DupRemover(const char *filename,const char *nodup)
{/* this basically makes the phone file and then removes duplicates from "filename" and makes a new file without the dupes*/
char phonefilename[10]="phone.all";
j=0;
ofstream fout3;
char stupidLine[lineSize];
phonelist(phonefilename,filename);
fout3.open(nodup,fstream::in | fstream::out);//| fstream::app);
ifstream phonefile(phonefilename,ios::in);
int h=0;
i=0;
while (phonefile.getline(phonenumber,12,'\n'))
{
ifstream filename21(filename, ios::in);
j=0;
int d=0;
while (filename21.getline(stupidLine, lineSize))
{
j++;
if (d==2)//it does it twice had to stop that
d=0;
if (d==0)
{
//fout5<<"inner loop "<<phonenumber<<" "<<j<<" "<<h<<endl;
// fout5<<stupidLine+258<<endl;
if ((strstr(stupidLine+258, phonenumber) !=0))//Only searches the first 270 columns
{
if (j<=h)
break;
fout3<<stupidLine<<endl;//Writes the whole line and returns to the next line
h=j;
break;
}
}
d++;
}
filename21.close();
}
fout3.close();
return 0;
}
int main (void)
{
char filename[12];
char nodup[12];
cout<<"Enter your processed file: ";
cin.getline (filename,11);
strncat (filename, ".all", 6);
cout << "Your File Name is " << filename << ".\n";
cout<<"Enter the new file name: ";
cin.getline(nodup,11);
strncat (nodup, ".all",11);
DupRemover(filename,nodup);
return(0);
}
Sorry for long post, the file consists of 501 characters in a row. Currently to process a 8 meg file it couldn't take upwards of 40 minutes and with a possiblity of 20 meg files I need a new way. I'm a beginner in programming so please bear with me. Any help would be great.