-6

How are you today. I have a question about file name handling :D. My code doesn't want to work it gives me false segmentation.

The idea is to make unique file name and create it if the the file with same name already exists. The first is the exception without file so the for loop starts from 1 instead of 0.

unistd.h is required!

char fileName[15][100];
sprintf(fileName[0],"output.txt");
FILE *output;

   for(int i = 1 ;i < 100; i++){
        sprintf(fileName[i],"Output%d.txt",i+1);

        //File exists
        if(!access(fileName[i],F_OK))
        {
            //Create and open a file
            output = fopen(fileName[i], "w");
            break;
        }    
    }

I just need to make a try function (like while) till it finds the name which is not taken. I just put the for loop there to have limit to 100.

  • 1
    So what's your question? – owacoder Dec 02 '15 at 16:39
  • 1
    *"create it **if** the the file with same name already exists."* If a file of that name already exists, then it isn't unique. – Weather Vane Dec 02 '15 at 16:41
  • You really need to read up on pointers. `&fileName[i]`? you're continually overwriting part of your `fileName` string with new stuff. and `fileName is a string, not an array of strings. `fileName[i]` makes no sense – Marc B Dec 02 '15 at 16:45
  • 1
    fileName is an array of strings. The issue is that it's being treated like a single string (passing a `char**` to sprintf instead of a `char*`) which should be an error if not a warning. Also no space is allocated for the strings themselves. – Kevin Dec 02 '15 at 16:46
  • Instead of `sprintf(&fileName[i],"Output%d.txt",i+1);`, use `sprintf(fileName,"Output%d.txt",i+1);` – R Sahu Dec 02 '15 at 16:52
  • 3
    ..ah... yet another one who doesn't allocate room for the strings and is amazed by the segmentation fault...Don't you guys learn about the heap and malloc? – Paul Ogilvie Dec 02 '15 at 16:57
  • `if (!access(filename, F_OK) ) { output = fopen( filename, "w" );}` creates a race condition. See http://stackoverflow.com/questions/24712383/how-to-avoid-race-condition-when-checking-if-file-exists-and-then-creating-it – Andrew Henle Dec 02 '15 at 17:48
  • Guys YOU ALL WERE wrong.. Well it was my fault to send wrong variation of the code. But the segmentation appears not because of fileName* I was at the beginning as static two dim array. Not a pointer. – Random Noob Dec 02 '15 at 18:20
  • Come on now, you've CHANGED THE QUESTION and told all these people trying to help they are wrong. – Weather Vane Dec 02 '15 at 18:23
  • You just guys are so mean to new programmers. Whatever. It wasn't the problem with dynamic and heap memory, so I will get the answer and post it for the future programmers. You can downvote me as much as you want... – Random Noob Dec 02 '15 at 19:22

3 Answers3

1

you have to malloc memory for the strings.

example

char** fileName;
int Number_Of_Strings, String_Length, i;
fileName = malloc(Number_Of_Strings * sizeof(char*));

for ( i = 0; i < Number_Of_String; i++)   // For each string
    fileName = malloc(String_Length * sizeof(char));

Number_If_Strings : How many strings do you need? String_Length : The length of each string

Dimos
  • 7
  • 1
  • Thanks but I don't want to allocate anything.. I have already allocated almost all the variables in a Program so I want to leave it static. Thanks for suggestion – Random Noob Dec 02 '15 at 18:18
1

What you have here:

char* fileName[100];

...is an array of 100 char pointers. However, you have not made space for the actual strings themselves.

When you do this:

sprintf(&fileName[0],"output.txt");

...you write "output.txt" to the location pointed to by the address in fileName[0], most likely 0x0 or some random value depending on your compiler and whether you allocate the array on the stack or the heap, etc. This normally results in a segmentation violation and your program terminates.

FrodeTennebo
  • 531
  • 3
  • 13
0

You did not allocate any memory for your array of pointers to point to. Also your syntax was a bit off here and there. This simple example shows how to allocate memory for an array of strings, assign some values, print them and free the memory.

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

#define MAXNAMES 10
#define MAXLEN   32

int main(void)
{
    char* fileName[MAXNAMES];
    int i;

    for(i=0; i<MAXNAMES; i++)
        if ((fileName[i] = malloc(MAXLEN)) == NULL)
            exit(1);

    sprintf(fileName[0],"Output.txt");     // your first name is an exception    
    for(i = 1; i<MAXNAMES; i++)            // the rest of the names
        sprintf(fileName[i], "Output%d.txt", i+1);    // using your offset of 1

    for(i = 0; i<MAXNAMES; i++)
        printf("%s\n", fileName[i]);

    for(i = 0; i<MAXNAMES; i++)
        free(fileName[i]);

    return 0;   
}

Program output:

Output.txt
Output2.txt
Output3.txt
Output4.txt
Output5.txt
Output6.txt
Output7.txt
Output8.txt
Output9.txt
Output10.txt
Weather Vane
  • 33,872
  • 7
  • 36
  • 56
  • Wait have you done all of them in one go? Am i right? I just need one try function (like while) till it finds the name which is not taken. I just put the for loop there to have limit to 100. – Random Noob Dec 02 '15 at 18:01
  • @RandomNoob this is a simplified example to show you how to use the string array. It was not intended to implement your exact problem, since I don't have the library functions you are using. – Weather Vane Dec 02 '15 at 18:19
  • Well the funniest point is that I have a working piece of code which was written half year ago and I just can't make variation of it – Random Noob Dec 02 '15 at 18:23
  • @RandomNoob I'm very happy that you find it funny wasting everybody's time, and putting them down when they have tried to help you. I'm splitting my sides with laughter now. – Weather Vane Dec 02 '15 at 18:26
  • why is it wasting of time. II still want to find the actual problem which cause segmentation. Im not trying insult you. Thank you for your time an deffort I really appreciate it! Just was on lecture and got back with -5 rating sure I will be upset of it – Random Noob Dec 02 '15 at 18:30
  • @RandomNoob it's wasting people's time because they answered the question you asked, but then changed, saying you didn't need those answers anyway. Bye bye. – Weather Vane Dec 02 '15 at 18:32