-2

Whenever i try to read the char from file and try to access it i am getting error . My code is :

while((c=fgetc(f))!=EOF) {
  if(c == '\n') {
    str[i]='\0';

    Info* inf = new Info();
    inf->getInputFromFile(str);
    if((table->insert(inf))==0)
    cout<<"exist."<<endl;
    //delete inf;

    for(int j =0; j<50;j++)
    str[j]='\0';

    i=0;

  } else {
    str[i] = c;
    i++;
}

But here when ever I try to give a dummy word such as : inf->getInputFromFile("Hello"); I am not getting any error. I can't also find the error using debuggin because when I press F8(CodeBlocks IDE) my debugger start and it doesn't stop any line. i.e. i don't find any error when running the program using F8.

Here is the input file content :

int, INTEGER
myFunction, FUNCTION
x, IDENTIFIER
5, NUMBER
x, Identity
char, CHARACTER
fl, FLOAT
dbl, DOUBLE
you, MEINTHIS
dlt, DELETE
hel, HELLOWORLD
string, STRING
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
Developer
  • 385
  • 1
  • 3
  • 18
  • so what should i do . Use EOF in next to while in If case? – Developer Jan 26 '14 at 19:43
  • Should i use instead this : while(1){ c=fgetc(f)); if(c == EOF){ break; } – Developer Jan 26 '14 at 19:44
  • Where are you getting the error? – Xyzk Jan 26 '14 at 19:45
  • I can't find out . Because i can't open it in debug mode. May be it is with the string as if i give this inf->getInputFromFile("Hello"); all things go right – Developer Jan 26 '14 at 19:46
  • If so, add printfs at the beginning of if, else, and while loop. Also, the problem might be that you do not end your string, if file ends without `\n` at the end of every line. – Xyzk Jan 26 '14 at 19:49
  • 1
    There are too many missing details within the question, which are required in order to detect the exact problem at hand, so will just give you a few advices: 1. Function `fgetc` returns an `int`, so declare variable `c` as such, and cast it to `char` where needed (if I'm not mistaken, `EOF` is an `int` and that could get your `while` loop running forever). 2. There doesn't seem to be any point in that `for` loop. 3. Are you sure you've opened the file (pointed by `f`) for reading? – barak manos Jan 26 '14 at 19:57
  • Can you tell me what other thing needed for clarificaiton. Cause i have tried ^ mention way but no result. – Developer Jan 26 '14 at 20:06
  • 1
    What are you doing here: `Info* inf = new Info();`? C doesn't have the `new` keyword. Edit: OK, I see: you are using C++. Then update your qestion tags. – pzaenger Jan 26 '14 at 20:18
  • Problem is not clear. – BLUEPIXY Jan 26 '14 at 21:55
  • Describe the error you are receiving. – chux - Reinstate Monica Jan 28 '14 at 03:19

1 Answers1

0

How to read a file in C was answered here: In C, how should I read a text file and print all strings And I think that's the answer you are searching for.

Community
  • 1
  • 1
Xyzk
  • 1,332
  • 2
  • 21
  • 36