opening and reading a file in c

 #include <stdio.h>


int main()
{
    FILE *ptr;
    int num1, num2;
    ptr = fopen("C:\\LearnC\\sample.txt", "r");

    fscanf(ptr, "%d", &num1);
    fscanf(ptr, "%d", &num2);
    if (ptr == NULL)
    {
        printf("The file does not exist");
    }
    printf("The value of num is %d\n", num1);
    printf("The value of num is %d\n", num2);
    fclose(ptr);
    return 0;
}

Comments

Popular posts from this blog

snake game Version-2 (using random number)

allocating dynamic memory using calloc

store a table in dynamic array and then change the size of array dynamically to store more numbers