Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I can use the following languages, C, Python, or Java. I am using I/O redirectio

ID: 3784467 • Letter: I

Question

I can use the following languages, C, Python, or Java.

I am using I/O redirection, and I am writing a program to read a series of names, one per line from standard input, and write out these names spelled in reverse order to standard output. Here are the steps.

a.    Input a series of names that are typed in from the keyboard and write them out, reversed, to a file called file1.

b.    Read the names in from file1; then write them out, re-reversed, to a file called file2.

c.    Read the names in from file2, reverse them again, and then sort the resulting list of reversed words.

I have this so far are file1.

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>

int main()
{
    /* A nice long string */
    char string[256];                             

    printf( "Please enter names in reverse: " );

    /* notice stdin being passed in */
    fgets ( string, 256, stdin );         

    printf( "You entered, %s", string );

    getchar();
}

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

And this is what I have for file2

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

FILE *ifp, *ofp;

int
main()
{
    char c;
    int fd;
    char filename[20];

    printf("Enter the name of the file: ");
    gets(filename);
    fd = open(filename, O_RDONLY, 0751);

    while(read(fd,&c,1) != 0)
    {
        write(stdout, &c, 1);
    }
    close(fd);
}
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

My file2 is asking what file it is going to read, but it won't display the revered names back on the screen. I need help!!

Explanation / Answer

#include <stdio.h>
#include <errno.h>

long countchars(FILE *);

void main(int arg0, char * arg1[])
{
int i;
long count;
char ch0, ch1;
FILE *file1, *file2;

if (file1 = fopen(arg1[1], "r"))
{
printf("The FILE has been opened");
file2 = fopen(arg1[2], "w");
count = countchars(file1);
fseek(file1, -1L, 2);   
printf("Number of characters copied %d ", ftell(file1));

while (count)
{
ch0 = fgetc(file1);
fputc(ch0, file2);
fseek(file1, -2L, 1);
count--;
}
printf(" **File reversed order** ");
}
else
{
perror("Error occured ");
}
fclose(file1);
fclose(file2);
}

long count_characters(FILE *f)
{
fseek(f, -1L, 2);
long last_pos = ftell(f);
last_pos++;
return last_pos;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote