I want the program can do that for me.
When I enter the name of file in the same folder of .exe, the program will scan the file data and print it out the data in order.
However, i do not know how to scan the data in the file and print it out.
And i do not know why i enter the file name( which in the same folder of .exe), and it shows that can't find the file.

Here is my code

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


int main(int argc, char* argv[])
{ 
    FILE * inputFile = NULL;
    char filename[FILENAME_MAX];
    char c = ' ';
    int number_of_data;
    int number;

    if(argc > 1)
    {
        strncpy(filename, argv[1], FILENAME_MAX);
    }
    else
    {
        puts("Press Enter the name of the File from which to read the data : ");
        fgets(filename, FILENAME_MAX, stdin);
    }


    inputFile = fopen(filename, "r");
    if(inputFile == NULL)
    {
        printf("Cannot open file %s Press Enter to quit", filename );
        getchar();
        return EXIT_FAILURE;
    }

    fscanf(inputFile, "%d", &number_of_data); 

    printf("%d", &number_of_data);

    while((c = (char)getc(inputFile)) != EOF)
    {
        if(c == ' ')
            c = ' ';
    }
    fclose(inputFile);
    puts("Press any key to Continue");
    getchar();
    return EXIT_SUCCESS;
}

what compiler and operating system are you using? If you are getting the error that the file is not found then the file is not where you think it is or the program isn't running in the same folder as the file. Put the full path to the file on the command line and see if that works.

Thanks for replying. I am running Visual studio and my os is win7. "Put the full path to the file on the command line and see if that works." so how to do that?

You can enter it on the command line or you can enter it when you ask the question and read it in with fgets or you change your program slightly and hard-code the path directly into your program (see below). Note that fgets leaves in a newline from the input and you need to get rid of that newline to concatenate correctly (again, see program below).

A newline character makes fgets stop reading, but it is considered a valid character by the function and included in the string copied to str.

http://www.cplusplus.com/reference/clibrary/cstdio/fgets/

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


int main(int argc, char* argv[])
{ 
    FILE * inputFile = NULL;
    char filename[FILENAME_MAX];
    char full_filename[FILENAME_MAX] = "C:/Documents and Settings/Vern/My Documents/Some Directory/";
    char c = ' ';
    int number_of_data;
    int number;
    if(argc > 1)
    {
        strncpy(filename, argv[1], FILENAME_MAX);
    }
    else
    {
        puts("Press Enter the name of the File from which to read the data : ");
        fgets(filename, FILENAME_MAX, stdin);
    }

    // Remove that pesky newline at the end.  Could be an '\r', could be an '\n'.  Just
    // replace any trailing white-space with a NULL terminator
    int filename_len = strlen(filename);
    while(filename_len > 0 && isspace(filename[--filename_len]))
    {
        filename[filename_len] = 0;
    }

    // now add the filename to the path
    strcat(full_filename, filename);


    inputFile = fopen(full_filename, "r");
    if(inputFile == NULL)
    {
        printf("Cannot open file %s Press Enter to quit", full_filename );
        getchar();
        return EXIT_FAILURE;
    }
    fscanf(inputFile, "%d", &number_of_data); 
    printf("%d", &number_of_data);
    while((c = (char)getc(inputFile)) != EOF)
    {
        if(c == ' ')
            c = ' ';
    }
    fclose(inputFile);
    puts("Press any key to Continue");
    getchar();
    return EXIT_SUCCESS;
}

how is argv getting the file name? However you do it make it the full path to the file. Otherwise, the file should be located in the same folder where the program's source files are located because that's where vc++ runs the program from.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.