Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,680 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,216 people online right now. Registration is fast and FREE... Join Now!




copy one file into another with line numbers add

 
Reply to this topicStart new topic

copy one file into another with line numbers add

lwlewis
post 9 Jun, 2008 - 07:43 PM
Post #1


New D.I.C Head

*
Joined: 15 Feb, 2008
Posts: 5

Hi everybody,

I have an assignment that I am trying to get finished and I cannot seem to find the code or commands to add line numbers to an output file. Here is the code that I have so far.

CODE


#include <stdio.h>
#include <stdlib.h>

int
main(int argc, char *argv[])
{
    FILE *input, *output;
    char ch;
    char name;
    
    name = strcat(argv[2], ".lis");
    
    input = fopen(argv[1], "r");
    if (input == NULL) {
        printf("\nCannot open file %s for input\n", argv[1]);
        exit(1);
    }
    output = fopen(name, "w");
    if (output == NULL) {
        printf("\nCannot open file %s for output\n", name);
        exit(1);
    }
    for(ch = getc(input); ch != EOF; ch = getc(input))
        strcpy("***************** %s ***************", name);
        putc(ch, output);
        
        fclose(input);
        fclose(output);
        printf("\nCopied %s to %s\n", argv[1], name);
        
        return(0);
}



what this needs to do is take a command line argument that is the name of a file and creates a new text file with a heading line of "*************** FILE NAME ***************" where file name is the name of the new file and the contents of the original file with line numbers added.

I understand that I may not have the strcpy built correctly. The name variable is the strcat of agrv[2] and ".lis" which are requirements for the assignment. The "strcpy" is where the header row is read into the new file before the old file is copied in. Should that just be "putc" then the sting to be read into the file? Any advise there would be appreciated also.
User is offlineProfile CardPM

Go to the top of the page

Martyr2
post 9 Jun, 2008 - 10:35 PM
Post #2


Programming Theoretician

Group Icon
Joined: 18 Apr, 2007
Posts: 5,062



Thanked 175 times

Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions


Here is a nice crude beginning for you. This should work nicely and give you the output you are looking for. Do remember though that you are tacking on a "lis" extension on the end of the second file so don't include an extension when using the command prompt.

cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char *argv[])
{
FILE *input, *output;
char ch;
char name[30];

// Store in the name and path, then append ".lis"
strcpy(name,argv[2]);
strcat(name, ".lis");

// Open the input
input = fopen(argv[1], "r");
if (input == NULL) {
printf("\nCannot open file %s for input\n", argv[1]);
exit(1);
}

// Open the output
output = fopen(name, "w");
if (output == NULL) {
printf("\nCannot open file %s for output\n", name);
exit(1);
}

// Print the header with the filename
fprintf(output,"***************** %s ***************\n",name);

// Setup our read line buffer and counter
char line[500];
int counter = 1;

// Read in a line up to 500 chars or until it hits a newline
fgets(line,500,input);

// While not at the end of a file
while (!feof(input)) {
// Read in a line up to 500 chars or until it hits a newline
// Then print it with a counter for the line number
fgets(line,500,input);
fprintf(output,"%d: %s",counter, line);
counter++;
}

// Cleanup the file pointers
fclose(input);
fclose(output);
printf("\nCopied %s to %s\n", argv[1], name);

return(0);
}


Oh and before you say the famous words "It doesn't work!" keep playing with it because yes it does work you are just not using it correctly.

Enjoy!

"At DIC we be file copying code ninjas... we also copy cats." decap.gif
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 9 Jun, 2008 - 10:38 PM
Post #3


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


There are few problems in your code. I am modifying your for loop to make it work
cpp


char str[100];
int line_no;

fprintf(output,"***************** %s ***************\n", name);
line_no = 0;
while(input != EOF || input!=NULL)
{
fgets(str,100,input);
line_no++;
fprintf(output,"%d : ",line_no);
fputs(str,output);
}




Please see for syntax errors. I hope this will help you. smile.gif
User is offlineProfile CardPM

Go to the top of the page

lwlewis
post 11 Jun, 2008 - 05:05 PM
Post #4


New D.I.C Head

*
Joined: 15 Feb, 2008
Posts: 5

QUOTE(AmitTheInfinity @ 9 Jun, 2008 - 11:38 PM) *

There are few problems in your code. I am modifying your for loop to make it work
cpp


char str[100];
int line_no;

fprintf(output,"***************** %s ***************\n", name);
line_no = 0;
while(input != EOF || input!=NULL)
{
fgets(str,100,input);
line_no++;
fprintf(output,"%d : ",line_no);
fputs(str,output);
}




Please see for syntax errors. I hope this will help you. smile.gif



Ihave got it to compile with only one warning "Comparison between pointer and integer" this is in reference to the while(input != EOF || input = NULL). Here is the complete code.

CODE

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char *argv[])
{
    FILE *input, *output;
    char ch;
    char name[30];
    
    strcpy(name, argv[2]);
    strcat(name, ".lis");
    

    input = fopen(argv[1], "r");
    if (input == NULL) {
        printf("\nCannot open file %s for input\n", argv[1]);
        exit(1);
    }
    output = fopen(name, "w");
    if (output == NULL) {
        printf("\nCannot open file %s for output\n", name);
        exit(1);
    }
    char str[100];
    int line_no;
           fprintf(output, "***************** %s ***************", name);
           line_no = 0;
           while(input != EOF || input != NULL)
           {
               fgets(str,100,input);
               line_no++;
               fprintf(output, "%d : ", line_no);
               fputs(str, output);
           }
  
        
        fclose(input);
        fclose(output);
        printf("\nCopied %s to %s\n", argv[1], name);
        
        return(0);
}


what would the problem be? also when it runs I receive The following error:

"6 [main] assignment_7_1 3812 _cygutls::handle_exceptions: Error while dumping state (probably corrupted stack)
/cygdrive/c/Users/Larry/.netbeans/6.1/bin/dorun.sh: line 103: 3812 Segmentation fault (core dumped) "$pgm" "$@" "

I use cgywin as my compiler with netbeans 6.1, is this a runtime error?
User is offlineProfile CardPM

Go to the top of the page

AmitTheInfinity
post 13 Jun, 2008 - 12:51 AM
Post #5


C Surfing ∞

Group Icon
Joined: 25 Jan, 2007
Posts: 1,015



Thanked 34 times

Dream Kudos: 125
My Contributions


I wrote that code on the fly so was not able to compile and see if it works. Wellwhile(input != EOF || input != NULL) try this instead while(!feof(input)).

even right now I am not at my desk so not able to compile and see the results so please go through the code with this change and see what happens. I hope others will get in and try to solve your other problems as well. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:45AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month