i have this piece of code that coppies a file to another: my question is, how do i change the name of the created output file ?
for example if the file that i run this code on is "text.txt" how do i add the ending ".something" to the file's name created so it'll be "text.txt.something" ?

#include<stdio.h>

int main(int argc, char *argv[])
{
  FILE *fp;
  void filecopy(FILE *, FILE *);
  char *prog = argv[0];

  if(argc ==1)
    filecopy(stdin, stdout);
  else
    while(--argc > 0)
        if((fp = fopen(*++argv, "r")) == NULL)
        {
            fprintf(stderr, "%s: can't open %s\n", prog, *argv);
            exit(1);
        }
        else {
            filecopy(fp, stdout);
            fclose(fp);
        }

    if(ferror(stdout))
    {
        fprintf(stderr, "%s: error writing stdout\n", prog);
        exit(2);
    }
    exit(0);
}

void filecopy(FILE *ifp, FILE *ofp)
{
  int c;

  while ((c = getc(ifp)) != EOF)
    putc(c, ofp);
}

I read your code 3 times and can't find a fopen for write. Just stdout.

So I think you are missing code before when it needs to do a little string work. That is, copy your argv to some string and concat what you want to that string, then open that for writing.

@rproffitt oh sorry my bad i forgot to add the lines, but still im getting segment fault(core dumped) when running it, although it compiles fine

int main(int argc, char **argv) {

    char *extension=".index";
    char *newFileName="";
    /* Initialize empty list */
    struct wordStruct *headptr = NULL;
    /* Initialize current */
    struct wordStruct *current;
    struct wordStruct *temp;
    FILE *fileptr = NULL;

    if ( NULL == ( fileptr = fopen ( "test.txt", "r"))) {
        fprintf ( stderr, "could not open file\n");
        return 0;
    }

    /* Insert words in list */
    headptr = read_words( fileptr, headptr);

    fclose ( fileptr);

   strcpy(newFileName, *argv);

   strcat(newFileName, extension );

    if ( NULL == ( fileptr = fopen ( newFileName, "w"))) {
        fprintf ( stderr, "could not open file\n");
        return 0;
    }

    current = headptr;
    while (current != NULL) {
        fprintf(fileptr,"%s appears in lines %s.\n", current->word, current->lines);
        current = current->next;
    }
    fclose (fileptr);
    /*release memory*/
    current = headptr;
    while (current != NULL) {
        temp = current;
        current = current->next;
        free ( temp->word);
        free ( temp->lines);
        free ( temp);
    }
    return 0;
}

by concat you mean using strcat and strcpy to add the extension (line's 20-24 im not sure if i've done it right) ?

and this will give segment fault (core dump):

char extension[]=".something";
char newFileName[]="";
   strcpy(newFileName, argv[1]);
   strcat(newFileName, extension );

   fileptr = fopen (newFileName, "w");

how do i do that correctly..?

Since this is C that line 2 where you declared the newFilename has an array with no space so it would fault on line 3.
Try simple like char newFileName[256];

@rproffitt i've just came up with this idea :

char* newFileName=malloc(strlen("text.txt")+strlen(".something")+1)

strcpy(newFileName, "text.txt");

strcat(newFileName, ".something");

FILE* output=fopen(newFileName, "w");

this wat at least it allocates exactly the needs for the name, but its giving me segment fualt when i run the program with no arguments although i've rewrote it like this:

int main(int argc, char **argv) {

    char* newFileName=malloc(strlen(argv[1])+strlen(".index")+1);
    /* Initialize empty list */
    struct wordStruct *headptr = NULL;
    /* Initialize current */
    struct wordStruct *current;
    struct wordStruct *temp;
    FILE *fileptr = NULL;
    if (argc <= 1) {
    fprintf ( stderr, "No arguments found! Program terminated.\n");
    exit(1);
    }
    else if (argc >= 3) {
    fprintf( stderr, "Too much arguments! Program terminated.\n");
    exit(2);
    }
    if ( NULL == ( fileptr = fopen ( argv[1], "r"))) {
        fprintf ( stderr, "could not open file\n");
        exit(3);
    }

    /* Insert words in list */
    headptr = read_words( fileptr, headptr);

    fclose ( fileptr);

strcpy(newFileName, argv[1]);
strcat(newFileName, ".index");

    if ( NULL == ( fileptr = fopen (newFileName, "w"))) {
        fprintf ( stderr, "could not open file\n");
        exit(3);
    }

    current = headptr;
    while (current != NULL) {
        fprintf(fileptr,"%s appears in lines %s.\n", current->word, current->lines);
        current = current->next;
    }
    fclose (fileptr);
    /*release memory*/
    current = headptr;
    while (current != NULL) {
        temp = current;
        current = current->next;
        free ( temp->word);
        free ( temp->lines);
        free ( temp);
    }
    return 0;
}

is there something wrong witht he check i made in line 10 ?

Your choice to work hard on getting the exact space it needs. Since it is seg faulting, your move. I know mine.

which space ? i didn't really get it..

this wat at least it allocates exactly the needs for the name,

If you work to allocate the exact space and it overflows, you get a seg fault. I offered a simple way but failed to convince you. Your choice here.

@rproffitt i gave up on the allocating idea cuz i've unerstood that even reallocating it won't solve the portability anyway.. as sizes differ in each system and can't be bothered to mess with "sizeof" ...
also i did searched for this and came accross solutions like this one..(the link you've put) but just wanted to mess with allocating, it's a dum curiousity reasing if u ask why lol
your solution is a nice elegant one since i know the max length of anyfile that my program will run on
pretty convinced right now Thanks !

Thanks for working at this. I understand the engineering view on this.

An engineer when they see a glass half full would tell us the glass is twice as big as it needs to be.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.