简体   繁体   中英

fopen() does not working well with gvim editor

First, I can't speak ENGLISH very well.. please understand :)

When I try to open text file to write something using fopen() with gvim, eg FILE *f = fopen("data.txt", "w"); , I can't find the data.txt file anywhere.. not only current directory but in Windows Explorer. BUT..It's working fine without the file. :|

I had doubts, So I've tried this FILE *f = fopen("c:\\my\\..."data.txt", "w"); Specify path, Working good!

I know that text file is stored in the same folder as in execution file folder.

Of course, Other IDE is working very well.(MSVS or Dev C++) What's my problem??

(I'm using Windows7 and gvim.)

// Modification
// source code

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

int main(void)
{
    FILE *f = NULL;
    int name[10];

    if((f = fopen("data.txt", "w")) == NULL){
        printf("sorry..\n");
        system("pause");
        exit(1);
    }
    fprintf(f, "%s\n", "Dennis");
    fclose(f);

    if((f = fopen("data.txt", "r")) == NULL){
        printf("sorry2..\n");
        system("pause");
        exit(1);
    }
    fscanf(f, "%s", name);
    printf("%s\n", name);

    fclose(f);
    system("pause");
    return 0;
} // I think it's no problem. I've tried different ways to try several times.

I will compile this -> map :! gcc % -o %<.exe (in vimrc. file) Push Cntl + F9. Vim 7.3, MingW 0.1-alpha-5. I just coding some source code, there are fopen(), fprintf() and fscanf(). When I tried to write on text file that I opened file, something was written there. but I could not see the text file. And, I tried to read the text file that I wrote file, although It was open, could not be read. Windows Explorer could not find the text file.

I can only guess, but I think your problem is that gvim doesn't change into the directory of your file.

There's an easy remedy for that: autochdir

Try to use Windows's search application, to locate your data.txt. If you started gvim from the start menu, they should be in "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Vim 7.3".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM