Hello, I am having some strings grammar problems.

char *eisodos1[50];
char e;
int *a;
int i;

int main()
{
  eisodos1[i]=getchar();  (1)
  e=eisodos1[i];             (2)
  if (eisodos1[i] == ' ') g++;    (3)

  while ((e!=EOF) && (a!=EOF));   (4)
}

Please don't look the code like a complete program, I posted the lines that I got a warning (not an error).

Ok first I am not sure about the declaration of strings and if I can declare them without saying the max number.
Here are the warnings:
(1) assignment makes pointer from integer without a cast (I dont even know whats a cast)
(2) assignment makes intenger from pointer without a cast
(3) comparison between pointer and integer
(4) is it correct?

Also by the way,

data = fopen("C:\\Program Files (x86)\\CodeBlocks\\mydata.dat","rb");
if (data == NULL)

the data isn't getting NULL, even the file doesn't exist!

Edited 6 Years Ago by skypower: n/a

eisodos1=getchar(); (1)
(1) assignment makes pointer from integer without a cast (I dont even know whats a cast)

eisodos1 is an array of pointers to char. getchar returns an integer. There's obviously a type mismatch here. You're trying to assign an integer to a pointer.

e=eisodos1; (2)
(2) assignment makes intenger from pointer without a cast

Same problem different direction. e is a char, eisodos1 is an array of pointers to char. You're trying to assign a pointer to a char.

if (eisodos1 == ' ') g++; (3)
(3) comparison between pointer and integer

Once again, type mismatch. eisodos1 is an array of pointers to char. ' ' is a char. The comparison is nonsensical.

while ((e!=EOF) && (a!=EOF)); (4)
(4) is it correct?

No. e is a char, a is a pointer to int. How can comparing them both against EOF (and integer macro) possibly work in any meaningful way?

the data isn't getting NULL, even the file doesn't exist!

I strongly doubt that you've discovered a bug in fopen. The problem is probably on your end, but you haven't provided nearly enough information for me to offer much beyond "bummer".

I strongly doubt that you've discovered a bug in fopen. The problem is probably on your end, but you haven't provided nearly enough information for me to offer much beyond "bummer".

I don't think that posting more code will help with this...

Once again, type mismatch. eisodos1 is an array of pointers to char. ' ' is a char. The comparison is nonsensical.

Ok but what happens if getchar() gets a space, how can I check that??

No. e is a char, a is a pointer to int. How can comparing them both against EOF (and integer macro) possibly work in any meaningful way?

My purpose here is to check the end of file but I won't be able to always know if the last getchar() gives char or int.


Also, thanks for helping!

I don't think that posting more code will help with this...

Note that I said "information", not "code". My choice of words is significant.

Ok but what happens if getchar() gets a space, how can I check that??

If getchar "gets" a space, it'll return ' '. I don't see how this is relevant since your problem is a type mismatch rather than improperly using getchar.

My purpose here is to check the end of file but I won't be able to always know if the last getchar() gives char or int.

getchar always returns an int, and at end-of-file that int will compare equal to the EOF macro. You can compare also that int against a character:

int ch;

while ((ch = getchar()) != EOF)
{
    if (ch == ' ')
    {
        puts("Space detected!");
    }
}

If you change

char *eisodos1[50];
to
char eisodos1[50];

then all three of the errors will disappear.

You want eisodos to be an array of characters, not an array of pointers to characters.

Eager thanks.

Narue,

while ((e!=EOF) && (a!=EOF));

I am very confused here. First of all is it bad to compare EOF with char or int? Then, writing int(e)!=EOF would have made any difference?

Also, do you have any ides what's wrong with the NULL problem I mentioned at the last lines?

Thanks again!

EOF is what is called an "out of band value'.

Ever value which can be stored in a "char" is a valid character. In order to have getchar() return a value which represent the End Of File, it needs to have a value which cannot be represented as a valid "char". "Int" has a larger range of values than "char"; EOF is one of them (usually -1). So to test for EOF, you need to save the value returned by getchar() in an "int" variable, then compare that to EOF.

No idea about the NULL issue -- as another respondent said, you haven't provided enough information to tell what might be the problem. Ignoring requests for additional information is impolite. How is "data" declared? What value does it have when you open an existing file and a non-existing file?

Comments
Great explanation :)

Hello, I am having some strings grammar problems.


Also by the way,

data = fopen("C:\\Program Files (x86)\\CodeBlocks\\mydata.dat","rb");
if (data == NULL)

the data isn't getting NULL, even the file doesn't exist!

Try to read the contents of the file. If you are able to read and print the contents then the file does exist. Also what is the data type of the variable data

FILE *data;
     data = fopen("C:\\Program Files (x86)\\CodeBlocks\\mydata.dat","rb");
     if (data == NULL)
     {
          printf("1\n");
          data = fopen("C:\\Program Files (x86)\\CodeBlocks\\mydata.dat","wb");
     }
     else
     {
          printf("2\n");
          data = fopen("C:\\Program Files (x86)\\CodeBlocks\\mydata.dat","ab");
     }

Here it is. Also what's a possible solution for the e and EOF? int(e)!=EOF doesn't work

Declare "int e;" See what Narue wrote.

What makes you think that "int(e)" means something in C?

So what is the value of "data" after the first fopen()?

Note that you never need to write code like this. If a file does not exist, fopen("filename","a") will create it an position the file pointer at the beginning.

Declare "int e;" See what Narue wrote.

What makes you think that "int(e)" means something in C?

So what is the value of "data" after the first fopen()?

Note that you never need to write code like this. If a file does not exist, fopen("filename","a") will create it an position the file pointer at the beginning.

I ment (int)e, I have seen it many times. Now about the file, I am not even sure what a does. I just wanted to open the file for writing but as far as I know if I give "wb" for mode, it deletes the contects... (by the way yes, I want it to be binary).

Don't know where you are seeing "int (e)". It isn't C. You've received multiple answers to your questions about EOF and getchar(). so far, it appears that you are ignoring them.

If you want advice here, take it. If you don't, don't post questions.

If you don't know what you want to do, it's very difficult to do it. Read any text book for the options to fopen, including "a". Or search online for "fopen".

Don't know where you are seeing "int (e)". It isn't C. You've received multiple answers to your questions about EOF and getchar(). so far, it appears that you are ignoring them.

If you want advice here, take it. If you don't, don't post questions.

If you don't know what you want to do, it's very difficult to do it. Read any text book for the options to fopen, including "a". Or search online for "fopen".

Lol i am not ignoring any advice. also check that

http://www.ozzu.com/programming-forum/converting-int-char-t266.html

I think you are over complicating a simple problem. Is this your problem:
You have a file which may or may not exist. If the file exists you have to append content to it. If the file does not exist, create the file and add content to it ?

This is how you want to open the file

FILE* fp;
fp= fopen("filename","ab");
/*
If in addition to appending to the file you also want to read from the file, you open the file like this      
fp= fopen("filename","ab+");
*/
if(fp==NULL)
{
     printf("Error\n");
}
else
{
     //Do what you want and dont forget to close the file after that
     fclose(fp);

}

Edited 6 Years Ago by abhimanipal: n/a

Again: EOF is not a value that can be stored in a "char". No matter what you do to cast the value of the "char" to an "int" it will not have the value EOF.

The reference you give is "converting an int to a char". You are trying to convert a char to an int".

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