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 ();
}

Shouldn't your path be

"c:\word"

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

You can add it to the project so it can reside with the exe.

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 ==========

First make sure do you have that perticular file on specified location which you are trying to open in read mode...

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!!!

hey which file you are trying to open..

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.

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.