English 中文(简体)
为什么在从主(argv[])获得价值时会失败
原标题:why does fopen fail when given value from main( argv[])
  • 时间:2011-09-28 11:35:48
  •  标签:
  • c
  • fopen

这是我的方案。

#include <ncurses.h>

int main( int argc, char *argv[] )
{
    initscr();
    FILE *fd;
    char *ProgFile;

    ProgFile = argv[1];
    printw(ProgFile);
    refresh();

    fd = fopen(ProgFile,"rb");

    if( fd==NULL )
    {
        printw("error");
        perror ("The following error occurred");
        refresh();
    }
    else
    {
        printw("bin file loaded:  %s ",ProgFile);
        refresh();
    }

    getch();
    endwin();

    return 0;
}

when run it given this error message: No such file or directory.

but if i hardcode ProgFile = "filemname.bin"; then the program works perfectly.

当节目使用两种版本的印刷文档名称时,Bin询问ProgFile的价值。

我一直试图用两天时间解决这一问题,对正在发生的事情没有任何想法。 谁能告诉我什么是错的?

this is c++ on linux centos

最佳回答

A program looks for relative filenames (those not starting with /) in its present working directory; this is inherited from the parent, which is the shell if you execute the program from the command line, and can be set explicitly when you run it from a starter. So, you need to make sure the program is started in the same directory that the file is in.

(你也可使用绝对途径,或采用明确的<代码>chdir)。 该系统称为档案的目录,但两者都很粗略,因此很难将方案围绕起来。

问题回答

First, this is C and not C++. I don t see any C++ in your code.

......

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

int main( int argc, char *argv[] )
{
    FILE *fd;
    char *ProgFile;

    ProgFile = argv[1];
    printf(ProgFile);

    fd=fopen(ProgFile,"rb");

    if( fd==NULL )
    {
        printf("error");
    }
    else
    {
        printf("bin file loaded:  %s ",ProgFile);
    }

    return 0;
}

对我来说,工作完全是罚款。 确保你通过正确的论点和正确的道路。 我建议你走整个道路,而不仅仅是filemname.bin





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签