Hello, i'm new to this forum and this's my 1st time posting a code. my issue is that i have a code that should read from a file called ( ex: word or text), but it does not read the file it jUst gives me the error (could not open the file) here is the code: ( I'M USING C NOT C++):

#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <process.h>
//#include <stdafx.h>
//#include <iostream>


int main (int argc, char *argv[])
{
    FILE *pFile;
    char name [9999];


    // open the file

    pFile = fopen("c:word", "r");


    if (pFile != NULL)
    {


        fscanf(pFile,"%s", name);

        fprintf("%s\n", name);

        fclose (pFile);
    }
    else
    {
        printf("Could not open the file.\n");

    }
    //exit(1);
    getch ();
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

Thx for replying, i tried "text.txt","r" did nt work too.

the question that i have next is where should i save the text file?? is it in the folder's project or in the debugger's folder or where ?? thx again

I did that too, and i even tried saving it in to debugger but still the same error on the black screen ( Could not open the file). here is my result of the output:

Compiling...
FileIO.c
.\FileIO.c(15) : warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(237) : see declaration of 'fopen'
.\FileIO.c(21) : warning C4996: 'fscanf': This function or variable may be unsafe. Consider using fscanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\stdio.h(256) : see declaration of 'fscanf'
.\FileIO.c(33) : warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
C:\Program Files\Microsoft Visual Studio 9.0\VC\include\conio.h(145) : see declaration of 'getch'
Linking...
Generating code
Finished generating code
Embedding manifest...
Build log was saved at "file://c:\Users\Snow\Documents\Visual Studio 2008\Projects\FileIO\FileIO\Release\BuildLog.htm"
FileIO - 0 error(s), 3 warning(s)
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

i think you program is just read data from the file and display the output on the screen, if its that simple then no need to use "char name [9999]" which is occupying 9999 bytes of memory. this could be done by just reading char by char from the file..

#include <stdio.h>
#include <process.h>

int main (void)
{
FILE *pFile;
char ch;


// open the file

if((pFile = fopen("text.txt", "r"))==NULL)
 {
   printf("\n Error: Can't open File \n"); 
   exit(0);
 }
 \\ Reading from file and displaying on screen..

 while((ch=fgetc(pFile))!=EOF)
     putchar(ch);

 fclose (pFile);

 return 0;
}

Hi, I tried saving the file everywhere ( in the project, debug and release) still does not read it. just for the info ( im using visual C++ 2008 and the file is saved as test.c not cpp. so where's the problem on that. couldn figure it put till now!!!

ok now try this save your text.txt file (which you are trying to open in read mode)at a perticular location like at C or D drive and specify that location in fopen like this...

pFile = fopen("D:\\text.txt", "r")

and check whether it opens or not.

Thx guys, i got it working. Microsoft visual 2008 creates two folders for the project; one for the main folder and the other one for the sub folder. so i put it in the sub folder and it worked.


special thx to vinitmittal2008.

hey u wan to open the text file u hv keep that txt file in the same project directory folder where is your project reside.it will give the required output.m sure u ll get the op. this was getting a problem to me also.

This article has been dead for over six months. Start a new discussion instead.