Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8314139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:43:32+00:00 2026-06-08T20:43:32+00:00

This is my code. I need create 10 files with this format: 0.txt 1.txt

  • 0

This is my code. I need create 10 files with this format:

0.txt
1.txt
…

But I can’t do it, the result is different. Can anyone help?

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


char* direccion(char *dirD,int number){
    char buffer[100];
    char *n;
    char *q;
    char* formato=".txt";

    sprintf(buffer, "%i", number);
    n= strcat(dirD,buffer);
    q= strcat(n,formato);

    return q;

}
int main(){
    int u;
    int number= 0;
    int t= 0;
    char* q = "e:/c9/";
    for(t=0; t< 10 ; t++){

        char* dir = direccion(q,number);

            if(fopen(dir,"w")){
                fopen(dir,"w");
                u=1;
            }
            else{
                u=0;
                printf("Error\n");
            }
        number++;

    }
    return 0;
}

Thanks !

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T20:43:33+00:00Added an answer on June 8, 2026 at 8:43 pm

    Problem No 1. is:

    char *q = "e:/c9/";
    for(t = 0; t < 10; t++)
    {
        char *dir = direccion(q, number);
    }
    

    The variable q is a string literal. However, in the function direccion(), you pass it as the first argument to strcat(), which tries to modify it — BOOM, a segfault. Solution:

    char buf[1000];
    strcpy(buf, "e:/c9/");
    char *dir = direccion(buf, number);
    

    Problem #2 is as @Charlie Martin pointed out is that you return a statically automatically allocated local string, which is invalid after the direccion() function returns. Maybe

    return strdup(buffer);
    

    in this case; don’t forget to free it after use.

    Edit: seems you don’t even ask about this. You can create a file using the open() system call (function):

    #include <fcntl.h>
    #include <stdio.h>
    
    int fd = open("file.txt", O_RDWR | O_CREAT, 0644);
    close(fd);
    

    Of course substitute the actual file mode you want for 0644 (but don’t make the file executable unless it contains a program to be executed).

    Edit 2: I didn’t even catch this one… So, in the for loop, you want to reset the base filename over and over:

    for (yadda yadda)
    {
        strcpy(buf, "e:/c9/");
        char *dir = direccion(buf, i);
    }
    

    etc.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With this code we can create a simple word document but I need to
I need to dynamically create textbox. This is my code, but with this I
I need to create a WPF grid dynamically from code behind. This is going
Kind of need help understanding what this code actually outputs. Does it out put
I have this unsafe code that I need to make safe so it can
I have this code and I need to make this work: if ($handle =
I need use this code: <%= button_tag :class => btn btn-primary do %> <%=
I need to change this code to cpp code This is the c# code
This code changes bg color only once. What do I need to add to
i have this code: def some_method(): #i need to get some attrs :( return

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.