Need help with C++ program

DarkKnight

Golden Member
Apr 21, 2001
1,197
0
0
This is what i need to do. I first need to read a file and determine how many lines are in the program. then I must allow the user to choose a couple of lines from the input file to delete. After the user has chosen the lines to delete, the program should create a new file with only the lines not chosen to be deleted. Any help?
 

Pakaderm

Senior member
Mar 8, 2001
519
0
0
1. I hope you know how to read in a file, FILE f = fopen(...) or better open an istream
2. Count the lines... you could use getline to do this, and count how many times you looped over the file
3. Prompt the user to enter a number in a loop until they enter -1 which means no more, so now you have a list of the lines that you want removed.
4. This step is just to run through the file calling getline, and only copy the line if it is not one of the lines the user chose.

This should be a very easy program to write, unless you are very new to C++/programming.

Post if you still need more help.

-Pakaderm
 

javathehut

Senior member
Oct 23, 2000
318
0
0
heres a start

#include <stdio.h>

int main(int nArg, char** pArgs)
{
if (nArg < 2)
{
printf("Enter filename as arguement");
return 1;
}

long nSize = 0, nLines = 0;
FILE* pFile = fopen(pArgs[1], "rw");

if (!pFile)
{
printf("Invalid File %s", pArgs[1]);
return 1;
}

fseek(pFile, 0, 2);
if (!(nSize = ftell(pFile)))
{
printf("Invalid file size for %s", pArgs[1]);
return 1;
}

char* szBuffer = new char[nSize];

fseek(pFile, 0, 0);
fread(szBuffer, 1, nSize, pFile);

while (nSize--)
{
if (*(szBuffer+nSize) == '\n')
++nLines;
}

delete szBuffer;
printf("File %s has %d lines\n", pArgs[1], nLines);
fclose(pFile);

return 0;
}
 

Pakaderm

Senior member
Mar 8, 2001
519
0
0
I was giving you hints, but I guess javathehut gave you most of the code... His examples uses FILE operations, but for C++ you really should use iostream.

-Pakaderm
 

HigherGround

Golden Member
Jan 9, 2000
1,827
0
0
well, just to add to javathehut's solution ...

- if you insist on using C for IO, at least use stat(...) to get the info about the file size and its existance ( rather than opening, and fseeking )
- if you allocate with _builtin_vec_new [] you should delete with [] ( delete [] szBuffer; )
- rather than looping several times through the file, make a vector<int> int which you dump the new line positions, when counting the line feeds, that way when you go write it out, you'll simple dump the buffer in two chunks ( from 0 to first LF poition and secod LF position to EOF )

basically it would look something like this


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <vector>

int main(int argc, char* argv[])
{
struct stat stbuf;
FILE* fp;
char* buf;
size_t sz, j;
vector<int> vc;
size_t range[] = { 0 , 0 };

if(stat(argv[1], &stbuf) != 0) {
perror("stat failed");
return 1;
}

if((fp = fopen(argv[1], "r")) == NULL) {
perror("fopen failed");
return 1;
}

buf = (char*)malloc(sizeof(char)*stbuf.st_size);
sz = read(fileno(fp), buf, stbuf.st_size);

vc.push_back(0);
for(j = 0; j<sz; j++) {
if(buf[j] == '\n')
vc.push_back(j+1);
}

fclose(fp);

if((fp = fopen(argv[2], "w")) == NULL) {
perror("fopen failed");
return 1;
}

printf("total lines: %d\n> ", vc.size( )-1);

scanf("%d %d", &range[0], &range[1]);
if(range[0] > 0 && range[1] < vc.size( ) && range[0] <= range[1]) {
fwrite(buf, sizeof(char), vc[range[0]-1], fp);
fwrite(buf+vc[range[1]], sizeof(char), sz-vc[range[1]], fp);
}
else {
fprintf(stderr, "invalid parameters\n");
return 1;
}

fclose(fp);
free(buf);
return 0;
}


edit: [ i ] got swallowed by the page parser :|
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |