in C coding language: Write a program \"increment\" that produces a file of sequ
ID: 3679559 • Letter: I
Question
in C coding language:
Write a program "increment" that produces a file of sequential numbers, one per line, by reading the last number in the file, adding one to it, then appending the incremented number to the file. Have your program take as its first command line argument the number of times it will repeat this operation, and as its second command line argument the file to read/increment.
For example, increment 10 foo.txt shall open and read the file foo.txt, search for the last line (verifying that it has an integer N on it), then add 10 more lines with N+1, N+2, etc.
Write another program "consecutive" that reads a file of numbers, one per line, then outputs and two adjacent lines for which the number in the second line is not exactly one greater than the number in the first line. It shall read from standard in and write to standard out.
Explanation / Answer
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
int i,j;
char fname[20];
int v;
ifstream file;
printf("Enter number of times it will repeat this operation, filename to read/increment. ");
scanf("%d", &i);
fgets(fname,20,stdin);
ifstream file;
file.open(fname,"w");
while(!file.eof())
{
file.getline(v,8);
}
fseek(fp,v, SEEK_END);//seeking the v value line in a file
//incrementing I times entered by the user
for(j=0;j<i;j++)
{
fprintf(fp,"%d",v++);
}
fclose(fp);
return o;
}
b)
#include <cstdlib>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[])
{
int count;
char prevline[10];
char thisline[10];
ifstream file;
file.open("foo.txt");
while(!file.eof())
{
prevline = NULL;
count = 1;
while ((thisline = readline(stream)) != NULL)
{
if (prevline == NULL)
{
// this is the first read from file
prevline = thisline;
continue;
}
if (strcmp(thisline, prevline) == 0)
{
count++;
} else // found a different line
if (count > 1) // but after I already counted several identical
{ // so I will print the line
printf(thisline);
count = 1;
}
free(prevline);
prevline = thisline;
}
if (count > 1) and (prevline != NULL)
{
printf(thisline);
}
free(prevline);
exit(1);
return o;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.