I know this is how you read from a file. I also want to print my output to the same name of the file plus ".txt". So if my file is called "text" I want to print it to a file called "text.txt".

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

int main(int argc, char* argv[1])
{
    FILE* Data2;
    Data2 = fopen(argv[1], "r");
    if (Data2 == NULL)
    {
       printf("ERROR");
       exit(100);
    }

    return 0;
}

Would it be something like this? Is it better to use "a" or "w" in the fopen? I want to write to it several times.

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

int main(int argc, char* argv[1])
{
    FILE* Data2;
    FILE* Data3;
    Data2 = fopen(argv[1], "r");
    Data3 = fopen(argv[1].txt, "a");
    if (Data2 == NULL)
    {
        printf("ERROR");
        exit(100);
    }
    fprintf(Data3, "\n Print something \n");
    fprintf(Data3, "\n Print something \n");
    fprintf(Data3, "\n Print something \n");

    return 0;
}

Dani AI

Generated

Common problems in the thread: the main prototype and the attempted argv[1].txt concatenation. int main(int argc, char *argv[1]) is invalid (use int main(int argc, char *argv[])) and C does not let you append a literal to argv[1] — build a new string instead. As noted, don’t try to concat in place; create an output filename and check for extension/identity with the input before opening files.

Mode choice: use "w" to truncate (fresh output each run) or "a" to append (preserve prior output). Both allow multiple fprintf calls in one run; pick "a" if previous contents must be kept, "w" to start over. Avoid opening the same pathname for both read and write unless the goal is in-place editing — the safe pattern is write to a separate file (or a temp file) and rename it into place.

Example (safe, small, and original) that adds “.txt” when needed and avoids clobbering the input if it already ends in “.txt”:

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

int main(int argc, char *argv[])
{
    if (argc < 2) { fprintf(stderr, "usage: %s infile\n", argv[0]); return 1; }
    const char *in = argv[1];
    size_t n = strlen(in);
    size_t outlen = n + 5; /* room for ".txt" and NUL */
    char *out = malloc(outlen);
    if (!out) { perror("malloc"); return 2; }

    if (n > 4 && strcmp(in + n - 4, ".txt") == 0)
        snprintf(out, outlen, "%.*s_out.txt", (int)(n - 4), in);
    else
        snprintf(out, outlen, "%s.txt", in);

    FILE *fin = fopen(in, "r");
    FILE *fout = fopen(out, "a"); /* or "w" to overwrite */
    if (!fin || !fout) { perror("fopen"); free(out); if (fin) fclose(fin); return 3; }

    fprintf(fout, "Example output line 1\n");
    fprintf(fout, "Example output line 2\n");

    fclose(fout);
    fclose(fin);
    free(out);
    return 0;
}

Notes: check return values, free buffers, and consider writing to a temporary file then using rename() (atomic replace) if the intent is to replace the original. This avoids subtle read/write corruption and mirrors the safer approach hinted at by .

line 10: No, you can't concantinate C strings like that

char fname[255];
char *ptr;
strcpy(fname,argv[1]);
ptr = strchr(fname,'.'); // truncate extension
if( strcmp(ptr,".txt") == 0)
{
   printf("Error: can't write to the same file as input file\n");
}
if( ptr != NULL)
   *ptr = '\0';
strcat(fname,".txt");
Data3 = fopen(fname,"w");
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.