The r + mode in C for files does not create the file if it does not exist? [duplicate]

1

I'm working on this code below and would like to store some random vectors in a file. From what I've read r + reads the file and writes to it, and if it does not exist it will be created. Well ... It turns out that when I use the code in the way it is it can not create a file, it just goes into the conditional that defines an error when creating the file, but when I change the mode from "r +" to "w +", it creates the file and works fine, I wonder if what I read about "r +" is wrong or is something that is wrong in my program and I am not seeing.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define tam 1000000

int main (){
    //declaração de variáveis
    FILE * arq = NULL;
    int i,array_size,v[tam];

    //lendo o tamanho do vetor e a quatidade de vetores a serem gerado
    printf("Digite o tamanho do vetor a ser gerado : \n");
    scanf ("%d",&array_size);

    //criando arquivo para armazenar os vetores gerados
    arq = fopen("vetoresgerados","r+");
    if (arq == NULL){
        printf ("Erro ao abrir o aquivo\n");
        return 0;
    }

    //gerando vetores aleatórios
    srand (time(0));
    for (i = 0; i < array_size;i++){
        v[i] = rand()%1000;
    }

    for (i = 0; i < array_size;i++){
        fprintf(arq, "%d ",v[i]);
    }
    fclose(arq);
    return 0;
}
    
asked by anonymous 07.09.2017 / 14:57

1 answer

0
  

"r+" Open file for read and update

     

"w+" Open the file to write and update, if the file does not exist it creates.

Reference: link

    
07.09.2017 / 15:05