Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 160 characters in body
Source Link
Luke
  • 2.5k
  • 9
  • 40
  • 67

In a C/C++ program, is it correct for me to do this:

int i;
FILE **files = malloc(numFiles * sizeof(FILE *));
std::string file("foo"), ext(".bar");
char *num[10];

for (i = 0; i < numFiles; i++) {
    files[i] = fopen((file + itoa(i, num, 10) + ext).c_str(), "w");
}

This is basically what I am doing, but I am not getting anything written to the files. They're blank.

EDIT

I have fixed my problem. I thought I might be doing something wrong here, but it turned out to be elsewhere. Thanks for the responses, anyway.

In a C/C++ program, is it correct for me to do this:

int i;
FILE **files = malloc(numFiles * sizeof(FILE *));
std::string file("foo"), ext(".bar");
char *num[10];

for (i = 0; i < numFiles; i++) {
    files[i] = fopen((file + itoa(i, num, 10) + ext).c_str(), "w");
}

This is basically what I am doing, but I am not getting anything written to the files. They're blank.

In a C/C++ program, is it correct for me to do this:

int i;
FILE **files = malloc(numFiles * sizeof(FILE *));
std::string file("foo"), ext(".bar");
char *num[10];

for (i = 0; i < numFiles; i++) {
    files[i] = fopen((file + itoa(i, num, 10) + ext).c_str(), "w");
}

This is basically what I am doing, but I am not getting anything written to the files. They're blank.

EDIT

I have fixed my problem. I thought I might be doing something wrong here, but it turned out to be elsewhere. Thanks for the responses, anyway.

Source Link
Luke
  • 2.5k
  • 9
  • 40
  • 67

How can I create array of files in C/C++?

In a C/C++ program, is it correct for me to do this:

int i;
FILE **files = malloc(numFiles * sizeof(FILE *));
std::string file("foo"), ext(".bar");
char *num[10];

for (i = 0; i < numFiles; i++) {
    files[i] = fopen((file + itoa(i, num, 10) + ext).c_str(), "w");
}

This is basically what I am doing, but I am not getting anything written to the files. They're blank.