×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Reading single Char from String in C.

Reading single Char from String in C.

Reading single Char from String in C.

(OP)
I am new to C and I'm having a problem with something I thought would be relatively simple.

I am trying to read a character at a time from a string, using the getchar() function, and I seem to be having alot of trouble.
Below is my code:

*---------------------------------------------------------

FILE  *fp0;
char  c(10+1);
int   j, ch;

fopen(fp0, "outfilename","w");

for ( j=0 ; (j<10) && ((ch=getchar()) != EOF) && (ch != '/n') ; j++)
{
   c[j] = (char)ch;
   fprintf(fp0, "c = %c/n", c[j]);
}

fclose(fp0);

*---------------------------------------------------------

This compiles okay, but hangs up when it's executed.

Any help would be greatly appreciated.

Regards,
mjs84

RE: Reading single Char from String in C.

(OP)
Oops:

I forgot to mention.  The string I am trying to read is in a string variable.

char   str1[10+1];

str1 = "123H.456";

RE: Reading single Char from String in C.

char  c(10+1);

Is this kind of initialization even allowed? (parentheses as opposed to brackets)

RE: Reading single Char from String in C.

Also, it's been a long time since reaing K&R, but I'm not so sure this is a good idea (will work in most cases, though)...

(ch=getchar()) != EOF) && (ch != '/n')

Can't remember if order those pieces are computed in are guaranteed to be left to right, and if not, (ch != '/n') may not do what you think it should do.

RE: Reading single Char from String in C.

(OP)
oops again.
I had initialized it
char c[10+1];

I did get this to work by writing my string into a temporary file and using the getc() function to read each character of the file.  

This seems to work okay.

If there are any better ideas, I would sure like to hear them.

Regards,
mjs84

RE: Reading single Char from String in C.

You might try changing your /n to \n.  

RE: Reading single Char from String in C.

Hi-

I think that the problem is the castings of the variables.
ch is cast as int.  In your for statement by doing
comparisons as characters, the program seems to work
correctly. I've modified you program (I hope that the
formatting doesn't get screwed up......

Hint: When in doubt, throw in a printf statement

#include <stdio.h>
main(){
                                                                                             
                                                                                             
FILE  *fp0;
char  c[10+1];
int   j, ch;
                                                                                             
fp0 = fopen("outfilename","w");
                                                                                             
//for ( j=0 ; (j<10) && ((ch=getchar()) != EOF) && (ch !='\n') ; j++)
for ( j=0 ; (j<10) && ((char)(ch=getchar()) != EOF) && ((char)ch !='\n') ; j++)
{
   c[j] = (char)ch;
//   printf( "c = %c\n", c[j]);
   fprintf(fp0, "c = %c\n", c[j]);
}
                                                                                             

fclose(fp0);
                                                                                             
}

RE: Reading single Char from String in C.

(OP)
richs

Thanks for the help.  I'll try it and let you know if that does it.

Thanks to all who give this valuable advice.

mjs84

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members!


Resources