i'm working on visual studio 2008. I'm trying use fopen to open txt files and it does not give any errors but its not doing the required task either. i've to submit this project on monday! please help
here's part of the code:

#include <stdio.h>
#include<iostream>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include<dos.h>
using namespace std;
FILE *file_ptr;
node* treefromfile()
{
 node *ptree=NULL;
 char word[20],meaning[100],str[120],*i;
 int flags=0;
file_ptr=fopen("C:\Dictionary","r");
 if(file_ptr==NULL)
  ptree=NULL;
 else
 {

  while(!feof(file_ptr))
  {
	i=fgets(str,120,file_ptr);
	if(i==NULL)
	break;
	seperateword(str,word,meaning);
	if(flags==0)
	{
	 ptree=maketree(word,meaning);
	 flags=1;
	}
	else
	 addword(ptree,word,meaning);
  }

Recommended Answers

All 7 Replies

the program does not go into the else statement, meaning it does not read the txt file.

Change
file_ptr=fopen("C:\Dictionary","r");
to
file_ptr=fopen("C:/Dictionary","r");

It still doesn't work. The output says: File does not exist or dictionary is empty...
Here's the complete code:

#include <stdio.h>
#include<iostream>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include<dos.h>
using namespace std;
#define LEFT  1
#define RIGHT 2

struct node
{
 char word[20],meaning[100];
 node *left,*right;
};

node *maketree(char[],char[]);

node* treefromfile();
void filefromtree(node*);
void addword(node*,char[],char[]);
void seperateword(char[],char[],char[]);
void displayall(node*);
node* bsearch(node*,char[]);
void showmenu();
FILE *file_ptr;

void prog()
{
 //clrscr();
 char word[20],meaning[100];
 int menuchoice;
 node *temp;
 temp=treefromfile();
 if(temp==NULL)
 {
  printf("File does not exist or dictionary is empty...");
  //getch();
 }
 while(1)
 {
  //clrscr();
  showmenu();
  scanf("%d",&menuchoice);
  switch(menuchoice)
  {
   case 1:printf("Enter word : ");
	  scanf("%s",word);
	  printf("Enter meaning : " );
	  flushall();
	  gets(meaning);
	  if(temp==NULL)
	   temp=maketree(word,meaning);
	  else
	   addword(temp,word,meaning);
	  break;
   case 2:if(temp==NULL)
	   printf("The dictionary is empty...");
	  else
	  {
	   printf("Find meaning of : ");
	   flushall();
	   gets(word);
	   node *t;
	   t=bsearch(temp,word);
	   if(t==NULL)
	    printf("Word not found...");
	   else
	   {
	    printf("%s : ",t->word);
	    puts(t->meaning);
	   }
	  }
	  //getch();
	  break;
   case 3:if(temp==NULL)
	   printf("Dictionary is empty...");
	  else
	   displayall(temp);
	  //getch();
	  break;
   case 4:filefromtree(temp);
	  exit(1);
	  break;
   default:cout<<"Enter Again";
	   //delay(1000);
	   prog();
	   break;
  }
 }
}
void showmenu()
{
 printf("	COMPUTER DICTIONARY");
 printf("[1].	Add a word.");
 printf("[2].	Find meaning.");
 printf("[3].	Display all.");
 printf("[4]. Save and Close.Enter Choice");
}
node* treefromfile()
{
 node *ptree=NULL;
 char word[20],meaning[100],str[120],*i;
 int flags=0;
 file_ptr=fopen("C:/Dictionary","r");
 if(file_ptr==NULL)
  ptree=NULL;
 else
 {

  while(!feof(file_ptr))
  {
	i=fgets(str,120,file_ptr);
	if(i==NULL)
	break;
	seperateword(str,word,meaning);
	if(flags==0)
	{
	 ptree=maketree(word,meaning);
	 flags=1;
	}
	else
	 addword(ptree,word,meaning);
  }

  fclose(file_ptr);
 }
 return ptree;
}
node* maketree(char w[],char m[])
{
 node *p;
 p=new node;
 strcpy(p->word,w);
 strcpy(p->meaning,m);
 p->left=NULL;
 p->right=NULL;
 return p;
}
void seperateword(char str[],char w[],char m[])
{
 int i,j;
 for(i=0;str[i]!=' ';i++)
  w[i]=str[i];
 w[i++]=NULL;	//Append the null and skip the space.
 for(j=0;str[i]!=' ';i++,j++)
 {
  m[j]=str[i];
 }
 m[j]=NULL;
}
void addword(node *tree,char word[],char meaning[])
{
 node *p,*q;
 p=q=tree;
 while(strcmp(word,p->word)!=0 && p!=NULL)
 {
  q=p;
  if(strcmp(word,p->word)<0)
   p=p->left;
  else
   p=p->right;
 }
 if(strcmp(word,q->word)==0)
 {
  printf("This word already exists...");
  //delay(1000);
 }
 else if(strcmp(word,q->word)<0)
  q->left=maketree(word,meaning);
 else
  q->right=maketree(word,meaning);
}
node* bsearch(node *tree,char word[])
{
 node *q;
 q=tree;
 while(q!=NULL)
 {
  //p=q;
  if(strcmp(word,q->word)<0)
   q=q->left;
  else if(strcmp(word,q->word)>0)
   q=q->right;
  if(strcmp(word,q->word)==0)
   break;
 }
 return q;
}
void filefromtree(node *tree)
{
 void travandwrite(node*);
 file_ptr=fopen("C:/Dictionary","w");
 if(file_ptr==NULL)
 {
  printf("Cannot open file for writing data...");
 }
 else //if(tree==NULL)
 {
  if(tree!=NULL)
  {
   travandwrite(tree);
  }
  fclose(file_ptr);  //Close the file anyway.
 }
}
void travandwrite(node *tree)
{
 if(tree!=NULL)
 {
  fprintf(file_ptr,"%s %s",tree->word,tree->meaning);
  travandwrite(tree->left);
  travandwrite(tree->right);
 }
}
void displayall(node *tree)
{
 if(tree!=NULL)
 {
  displayall(tree->left);
  printf("%s : %s",tree->word,tree->meaning);
  displayall(tree->right);
 }
}

void intro()
{
int i;
//clrscr();
//gotoxy(20,20);
cout<<"DICTIONARY LOADING";
for(i=0;i<50;i++)
{
 //gotoxy(15+i,21);
 cout<<"þþþ";
 //gotoxy(20,22);
 cout<<2*i<<"% completed";
 //delay(150);
}
//gotoxy(20,20);
cout<<"DICTIONARY LOADING COMPLETED";
//clrscr();
}
void main()
{
//clrscr();
intro();
prog();
}

The output says: File does not exist or dictionary is empty...

While that might be true, you can't be sure because your code is making an assumption about why the pointer is null. Try using perror() for more detailed information:

file_ptr=fopen("C:\Dictionary","r");

if (file_ptr == NULL) {
    perror(NULL);
    return NULL;
}

However, I can safely say right now that it looks like you're trying to open a folder rather than a file. Unless "C:\Dictionary" refers to an actual file without an extension, the error will be "File not found".

Is the name of the file actually "Dictionary" or "Dictionary.txt" ?

Also, with Vista above you actually can't read/write files in the root directory (C:) without admin priviledges.

Run your project as an administrator and see what I mean.

If pseudorandom21 does not have the answer to this, it could be that you've got the Dictionary file open with another utility that is causing a sharing violation.

Aside from that, putting files in the root is not a good idea (unless this was just a test).

its opening the txt file now but the code still wont run, its giving violation errors at runtime now.
here's the new code:

#include <stdio.h>
#include<iostream>
#include <conio.h>
#include <string>
#include <stdlib.h>
#include<dos.h>
using namespace std;
#define LEFT  1
#define RIGHT 2

struct node
{
 char word[20],meaning[100];
 node *left,*right;
};

node *maketree(char[],char[]);

node* treefromfile();
void filefromtree(node*);
void addword(node*,char[],char[]);
void seperateword(char[],char[],char[]);
void displayall(node*);
node* bsearch(node*,char[]);
void showmenu();
FILE *file_ptr;

void prog()
{
 //clrscr();
 char word[20],meaning[100];
 int menuchoice;
 node *temp;
 temp=treefromfile();
 if(temp==NULL)
 {
  printf("File does not exist or dictionary is empty...");
  //getch();
 }
 while(1)
 {
  //clrscr();
  showmenu();
  scanf("%d",&menuchoice);
  switch(menuchoice)
  {
   case 1:printf("Enter word : ");
	  scanf("%s",word);
	  printf("Enter meaning : " );
	  flushall();
	  gets(meaning);
	  if(temp==NULL)
	   temp=maketree(word,meaning);
	  else
	   addword(temp,word,meaning);
	  break;
   case 2:if(temp==NULL)
	   printf("The dictionary is empty...");
	  else
	  {
	   printf("Find meaning of : ");
	   flushall();
	   gets(word);
	   node *t;
	   t=bsearch(temp,word);
	   if(t==NULL)
	    printf("Word not found...");
	   else
	   {
	    printf("%s : ",t->word);
	    puts(t->meaning);
	   }
	  }
	  //getch();
	  break;
   case 3:if(temp==NULL)
	   printf("Dictionary is empty...");
	  else
	   displayall(temp);
	  //getch();
	  break;
   case 4:filefromtree(temp);
	  exit(1);
	  break;
   default:cout<<"Enter Again";
	   //delay(1000);
	   prog();
	   break;
  }
 }
}
void showmenu()
{
 printf("	COMPUTER DICTIONARY");
 printf("[1].	Add a word.");
 printf("[2].	Find meaning.");
 printf("[3].	Display all.");
 printf("[4]. Save and Close.Enter Choice");
}
node* treefromfile()
{
 node *ptree=NULL;
 char word[20],meaning[100],str[120],*i;
 int flags=0;
 file_ptr=fopen("E:\Dictionary1.txt","r");
 if(file_ptr==NULL)
  ptree=NULL;
 else
 {

  while(!feof(file_ptr))
  {
	i=fgets(str,120,file_ptr);
	if(i==NULL)
	break;
	seperateword(str,word,meaning);
	if(flags==0)
	{
	 ptree=maketree(word,meaning);
	 flags=1;
	}
	else
	 addword(ptree,word,meaning);
  }

  fclose(file_ptr);
 }
 return ptree;
}
node* maketree(char w[],char m[])
{
 node *p;
 p=new node;
 strcpy(p->word,w);
 strcpy(p->meaning,m);
 p->left=NULL;
 p->right=NULL;
 return p;
}
void seperateword(char str[],char w[],char m[])
{
 int i,j;
 for(i=0;str[i]!=' ';i++)
  w[i]=str[i];
 w[i++]=NULL;	//Append the null and skip the space.
 for(j=0;str[i]!=' ';i++,j++)
 {
  m[j]=str[i];
 }
 m[j]=NULL;
}
void addword(node *tree,char word[],char meaning[])
{
 node *p,*q;
 p=q=tree;
 while(strcmp(word,p->word)!=0 && p!=NULL)
 {
  q=p;
  if(strcmp(word,p->word)<0)
   p=p->left;
  else
   p=p->right;
 }
 if(strcmp(word,q->word)==0)
 {
  printf("This word already exists...");
  //delay(1000);
 }
 else if(strcmp(word,q->word)<0)
  q->left=maketree(word,meaning);
 else
  q->right=maketree(word,meaning);
}
node* bsearch(node *tree,char word[])
{
 node *q;
 q=tree;
 while(q!=NULL)
 {
  //p=q;
  if(strcmp(word,q->word)<0)
   q=q->left;
  else if(strcmp(word,q->word)>0)
   q=q->right;
  if(strcmp(word,q->word)==0)
   break;
 }
 return q;
}
void filefromtree(node *tree)
{
 void travandwrite(node*);
 file_ptr=fopen("E:\Dictionary1.txt","w");
 if(file_ptr==NULL)
 {
  printf("Cannot open file for writing data...");
 }
 else //if(tree==NULL)
 {
  if(tree!=NULL)
  {
   travandwrite(tree);
  }
  fclose(file_ptr);  //Close the file anyway.
 }
}
void travandwrite(node *tree)
{
 if(tree!=NULL)
 {
  fprintf(file_ptr,"%s %s",tree->word,tree->meaning);
  travandwrite(tree->left);
  travandwrite(tree->right);
 }
}
void displayall(node *tree)
{
 if(tree!=NULL)
 {
  displayall(tree->left);
  printf("%s : %s",tree->word,tree->meaning);
  displayall(tree->right);
 }
}

void intro()
{
int i;
//clrscr();
//gotoxy(20,20);
cout<<"DICTIONARY LOADING";
for(i=0;i<50;i++)
{
 //gotoxy(15+i,21);
 cout<<"þþþ";
 //gotoxy(20,22);
 cout<<2*i<<"% completed";
 //delay(150);
}
//gotoxy(20,20);
cout<<"DICTIONARY LOADING COMPLETED";
//clrscr();
}
void main()
{
//clrscr();
intro();
prog();
}

it's giving error in this line: w=str; of function 'seperate word'

and the Dictionary file i'm using as a test file has includes this:

ball gaind
bat balla
bell ghanti
cat billi

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.