Why does getw return -1 when trying to read a character?problem with flushing input stream CC read file line by line.dat structured file handling in C (manual ?)Why does sizeof(x++) not increment x?Why does fgetc function adds a carriage returnhow to add new line character between #include<stdio.h> and int main() Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does GCC generate 15-20% faster code if I optimize for size instead of speed?Weird bug using fopen() inside a functionfscanf csv in C. Value not assigned
Cuban Primes
Why does Taylor’s series “work”?
When the match time is called, does the current turn end immediately?
When did Britain learn about American independence?
Why would company (decision makers) wait for someone to retire, rather than lay them off, when their role is no longer needed?
Why aren't satellites disintegrated even though they orbit earth within their Roche Limits?
Why does the U.S military use mercenaries?
FIFO data structure in pure C
Would it be fair to use 1d30 (instead of rolling 2d20 and taking the higher die) for advantage rolls?
Why did nobody know who the Lord of this region was?
What do astronauts do with their trash on the ISS?
Why can't I share a one use code with anyone else?
Solenoid fastest possible release - for how long should reversed polarity be applied?
How could it be that 80% of townspeople were farmers during the Edo period in Japan?
bash: Counting characters within multiple files
Can a person still be an Orthodox Jew and believe that the Torah contains narratives that are not scientifically correct?
Bash grep result from command whole line
Was the dragon prowess intentionally downplayed in S08E04?
I recently started my machine learning PhD and I have absolutely no idea what I'm doing
Usage of the relative pronoun "dont"
What color to choose as "danger" if the main color of my app is red
multiline equation inside a matrix that is a part of multiline equation
What are the effects of eating many berries from the Goodberry spell per day?
What dog breeds survive the apocalypse for generations?
Why does getw return -1 when trying to read a character?
problem with flushing input stream CC read file line by line.dat structured file handling in C (manual ?)Why does sizeof(x++) not increment x?Why does fgetc function adds a carriage returnhow to add new line character between #include<stdio.h> and int main() Why does the C preprocessor interpret the word “linux” as the constant “1”?Why does GCC generate 15-20% faster code if I optimize for size instead of speed?Weird bug using fopen() inside a functionfscanf csv in C. Value not assigned
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I hoped I will see the result 65 since 65 is the ASCII value of A.
Why am I getting -1 instead?
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getw(fp);
printf("%dn",x);
return 0;
c
add a comment |
I hoped I will see the result 65 since 65 is the ASCII value of A.
Why am I getting -1 instead?
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getw(fp);
printf("%dn",x);
return 0;
c
2
How did you even hear aboutgetw? This suggests something very very wrong with whatever material you're learning from...
– R..
May 11 at 19:04
add a comment |
I hoped I will see the result 65 since 65 is the ASCII value of A.
Why am I getting -1 instead?
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getw(fp);
printf("%dn",x);
return 0;
c
I hoped I will see the result 65 since 65 is the ASCII value of A.
Why am I getting -1 instead?
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getw(fp);
printf("%dn",x);
return 0;
c
c
edited May 11 at 14:39
Dukeling
45.6k1063108
45.6k1063108
asked May 11 at 11:49
play storeplay store
693
693
2
How did you even hear aboutgetw? This suggests something very very wrong with whatever material you're learning from...
– R..
May 11 at 19:04
add a comment |
2
How did you even hear aboutgetw? This suggests something very very wrong with whatever material you're learning from...
– R..
May 11 at 19:04
2
2
How did you even hear about
getw? This suggests something very very wrong with whatever material you're learning from...– R..
May 11 at 19:04
How did you even hear about
getw? This suggests something very very wrong with whatever material you're learning from...– R..
May 11 at 19:04
add a comment |
3 Answers
3
active
oldest
votes
You are writing one byte to the file, but then try to read sizeof(int) bytes back from it. getw() returns EOF because of that, and the value of EOF is -1.
For this reason, when using getw() you should examine the file handle using ferror() to be able to tell whether the value getw() returned is the value read from the file or an error code.
Or better yet, use fread() to read from files. getw() is an old function that's there for compatibility with old code. Or use getc() or fgetc() instead, which always return an unsigned char cast to an int and thus EOF can be easily distinguished:
int x = getc(fp);
if (x == EOF)
fputs("Error reading from file.n", stderr);
else
printf("%dn",x);
Would using getc and storing the value in a char fix this?
– jackw11111
May 11 at 12:00
@jackw11111 Yes,getc()will work.
– Nikos C.
May 11 at 12:06
6
@jackw11111 getc yes, char no. getc returns an int (to accommodate EOF).
– Clifford
May 11 at 12:19
add a comment |
Oh, I see you have a problem right there with "w" just change "w" to "c" and smile. Actually, you're on the right track. Just change getw() to getc().
The output is -1 because you are using getw() function to read a .txt file that contains a char whereas, getw() function reads an integer from file. So the right function to use is getc() function because you have a char in your .txt file. getc() function reads character from file.
Copy and paste even faster:
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getc(fp);
printf("%dn",x);
return 0;
This would output 65.
add a comment |
You are getting -1 because getw() reads the next word from the stream (the size of a word is the size of an int and may vary from machine to machine) but in you case, when it try to read word from file it encounters EOF and the end-of-file indicator for the stream is set and getw() returns EOF. Note that EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.
You should use putw() to write the file, if you want to use getw() to read the file.
To show you the difference in file when using putc() and putw() to write the file:
When using putc():
putc('A',fp);
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 1B May 11 18:02 first.txt
^
|
size: 1 byte
When using putw():
putw('A', fp); // the first parameter type of putw() is int and the character will implicitly promoted to int
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 4B May 11 18:00 first.txt
^
|
size: 4 bytes
If using putw():
#include <stdio.h>
int main(void)
FILE *fp;
fp = fopen("first.txt", "w");
if (fp == NULL)
fprintf (stderr, "Failed to open file for write");
return -1;
putw('A', fp); // add the error handling for putw()
fclose(fp);
fp = fopen("first.txt", "r");
if (fp == NULL)
fprintf (stderr, "Failed to open file for read");
return -1;
int x = getw(fp);
if (feof(fp))
printf ("End of file reachedn");
else if (ferror(fp))
printf ("Error occurredn");
else
printf ("%dn", x);
return 0;
Output:
# ./a.out
65
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56090058%2fwhy-does-getw-return-1-when-trying-to-read-a-character%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are writing one byte to the file, but then try to read sizeof(int) bytes back from it. getw() returns EOF because of that, and the value of EOF is -1.
For this reason, when using getw() you should examine the file handle using ferror() to be able to tell whether the value getw() returned is the value read from the file or an error code.
Or better yet, use fread() to read from files. getw() is an old function that's there for compatibility with old code. Or use getc() or fgetc() instead, which always return an unsigned char cast to an int and thus EOF can be easily distinguished:
int x = getc(fp);
if (x == EOF)
fputs("Error reading from file.n", stderr);
else
printf("%dn",x);
Would using getc and storing the value in a char fix this?
– jackw11111
May 11 at 12:00
@jackw11111 Yes,getc()will work.
– Nikos C.
May 11 at 12:06
6
@jackw11111 getc yes, char no. getc returns an int (to accommodate EOF).
– Clifford
May 11 at 12:19
add a comment |
You are writing one byte to the file, but then try to read sizeof(int) bytes back from it. getw() returns EOF because of that, and the value of EOF is -1.
For this reason, when using getw() you should examine the file handle using ferror() to be able to tell whether the value getw() returned is the value read from the file or an error code.
Or better yet, use fread() to read from files. getw() is an old function that's there for compatibility with old code. Or use getc() or fgetc() instead, which always return an unsigned char cast to an int and thus EOF can be easily distinguished:
int x = getc(fp);
if (x == EOF)
fputs("Error reading from file.n", stderr);
else
printf("%dn",x);
Would using getc and storing the value in a char fix this?
– jackw11111
May 11 at 12:00
@jackw11111 Yes,getc()will work.
– Nikos C.
May 11 at 12:06
6
@jackw11111 getc yes, char no. getc returns an int (to accommodate EOF).
– Clifford
May 11 at 12:19
add a comment |
You are writing one byte to the file, but then try to read sizeof(int) bytes back from it. getw() returns EOF because of that, and the value of EOF is -1.
For this reason, when using getw() you should examine the file handle using ferror() to be able to tell whether the value getw() returned is the value read from the file or an error code.
Or better yet, use fread() to read from files. getw() is an old function that's there for compatibility with old code. Or use getc() or fgetc() instead, which always return an unsigned char cast to an int and thus EOF can be easily distinguished:
int x = getc(fp);
if (x == EOF)
fputs("Error reading from file.n", stderr);
else
printf("%dn",x);
You are writing one byte to the file, but then try to read sizeof(int) bytes back from it. getw() returns EOF because of that, and the value of EOF is -1.
For this reason, when using getw() you should examine the file handle using ferror() to be able to tell whether the value getw() returned is the value read from the file or an error code.
Or better yet, use fread() to read from files. getw() is an old function that's there for compatibility with old code. Or use getc() or fgetc() instead, which always return an unsigned char cast to an int and thus EOF can be easily distinguished:
int x = getc(fp);
if (x == EOF)
fputs("Error reading from file.n", stderr);
else
printf("%dn",x);
edited May 11 at 12:13
answered May 11 at 11:57
Nikos C.Nikos C.
35.7k53968
35.7k53968
Would using getc and storing the value in a char fix this?
– jackw11111
May 11 at 12:00
@jackw11111 Yes,getc()will work.
– Nikos C.
May 11 at 12:06
6
@jackw11111 getc yes, char no. getc returns an int (to accommodate EOF).
– Clifford
May 11 at 12:19
add a comment |
Would using getc and storing the value in a char fix this?
– jackw11111
May 11 at 12:00
@jackw11111 Yes,getc()will work.
– Nikos C.
May 11 at 12:06
6
@jackw11111 getc yes, char no. getc returns an int (to accommodate EOF).
– Clifford
May 11 at 12:19
Would using getc and storing the value in a char fix this?
– jackw11111
May 11 at 12:00
Would using getc and storing the value in a char fix this?
– jackw11111
May 11 at 12:00
@jackw11111 Yes,
getc() will work.– Nikos C.
May 11 at 12:06
@jackw11111 Yes,
getc() will work.– Nikos C.
May 11 at 12:06
6
6
@jackw11111 getc yes, char no. getc returns an int (to accommodate EOF).
– Clifford
May 11 at 12:19
@jackw11111 getc yes, char no. getc returns an int (to accommodate EOF).
– Clifford
May 11 at 12:19
add a comment |
Oh, I see you have a problem right there with "w" just change "w" to "c" and smile. Actually, you're on the right track. Just change getw() to getc().
The output is -1 because you are using getw() function to read a .txt file that contains a char whereas, getw() function reads an integer from file. So the right function to use is getc() function because you have a char in your .txt file. getc() function reads character from file.
Copy and paste even faster:
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getc(fp);
printf("%dn",x);
return 0;
This would output 65.
add a comment |
Oh, I see you have a problem right there with "w" just change "w" to "c" and smile. Actually, you're on the right track. Just change getw() to getc().
The output is -1 because you are using getw() function to read a .txt file that contains a char whereas, getw() function reads an integer from file. So the right function to use is getc() function because you have a char in your .txt file. getc() function reads character from file.
Copy and paste even faster:
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getc(fp);
printf("%dn",x);
return 0;
This would output 65.
add a comment |
Oh, I see you have a problem right there with "w" just change "w" to "c" and smile. Actually, you're on the right track. Just change getw() to getc().
The output is -1 because you are using getw() function to read a .txt file that contains a char whereas, getw() function reads an integer from file. So the right function to use is getc() function because you have a char in your .txt file. getc() function reads character from file.
Copy and paste even faster:
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getc(fp);
printf("%dn",x);
return 0;
This would output 65.
Oh, I see you have a problem right there with "w" just change "w" to "c" and smile. Actually, you're on the right track. Just change getw() to getc().
The output is -1 because you are using getw() function to read a .txt file that contains a char whereas, getw() function reads an integer from file. So the right function to use is getc() function because you have a char in your .txt file. getc() function reads character from file.
Copy and paste even faster:
#include <stdio.h>
int main()
FILE *fp;
fp=fopen("first.txt","w");
putc('A',fp);
fclose(fp);
fp=fopen("first.txt","r");
int x=getc(fp);
printf("%dn",x);
return 0;
This would output 65.
edited May 11 at 15:49
answered May 11 at 12:28
pasignaturepasignature
1579
1579
add a comment |
add a comment |
You are getting -1 because getw() reads the next word from the stream (the size of a word is the size of an int and may vary from machine to machine) but in you case, when it try to read word from file it encounters EOF and the end-of-file indicator for the stream is set and getw() returns EOF. Note that EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.
You should use putw() to write the file, if you want to use getw() to read the file.
To show you the difference in file when using putc() and putw() to write the file:
When using putc():
putc('A',fp);
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 1B May 11 18:02 first.txt
^
|
size: 1 byte
When using putw():
putw('A', fp); // the first parameter type of putw() is int and the character will implicitly promoted to int
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 4B May 11 18:00 first.txt
^
|
size: 4 bytes
If using putw():
#include <stdio.h>
int main(void)
FILE *fp;
fp = fopen("first.txt", "w");
if (fp == NULL)
fprintf (stderr, "Failed to open file for write");
return -1;
putw('A', fp); // add the error handling for putw()
fclose(fp);
fp = fopen("first.txt", "r");
if (fp == NULL)
fprintf (stderr, "Failed to open file for read");
return -1;
int x = getw(fp);
if (feof(fp))
printf ("End of file reachedn");
else if (ferror(fp))
printf ("Error occurredn");
else
printf ("%dn", x);
return 0;
Output:
# ./a.out
65
add a comment |
You are getting -1 because getw() reads the next word from the stream (the size of a word is the size of an int and may vary from machine to machine) but in you case, when it try to read word from file it encounters EOF and the end-of-file indicator for the stream is set and getw() returns EOF. Note that EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.
You should use putw() to write the file, if you want to use getw() to read the file.
To show you the difference in file when using putc() and putw() to write the file:
When using putc():
putc('A',fp);
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 1B May 11 18:02 first.txt
^
|
size: 1 byte
When using putw():
putw('A', fp); // the first parameter type of putw() is int and the character will implicitly promoted to int
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 4B May 11 18:00 first.txt
^
|
size: 4 bytes
If using putw():
#include <stdio.h>
int main(void)
FILE *fp;
fp = fopen("first.txt", "w");
if (fp == NULL)
fprintf (stderr, "Failed to open file for write");
return -1;
putw('A', fp); // add the error handling for putw()
fclose(fp);
fp = fopen("first.txt", "r");
if (fp == NULL)
fprintf (stderr, "Failed to open file for read");
return -1;
int x = getw(fp);
if (feof(fp))
printf ("End of file reachedn");
else if (ferror(fp))
printf ("Error occurredn");
else
printf ("%dn", x);
return 0;
Output:
# ./a.out
65
add a comment |
You are getting -1 because getw() reads the next word from the stream (the size of a word is the size of an int and may vary from machine to machine) but in you case, when it try to read word from file it encounters EOF and the end-of-file indicator for the stream is set and getw() returns EOF. Note that EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.
You should use putw() to write the file, if you want to use getw() to read the file.
To show you the difference in file when using putc() and putw() to write the file:
When using putc():
putc('A',fp);
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 1B May 11 18:02 first.txt
^
|
size: 1 byte
When using putw():
putw('A', fp); // the first parameter type of putw() is int and the character will implicitly promoted to int
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 4B May 11 18:00 first.txt
^
|
size: 4 bytes
If using putw():
#include <stdio.h>
int main(void)
FILE *fp;
fp = fopen("first.txt", "w");
if (fp == NULL)
fprintf (stderr, "Failed to open file for write");
return -1;
putw('A', fp); // add the error handling for putw()
fclose(fp);
fp = fopen("first.txt", "r");
if (fp == NULL)
fprintf (stderr, "Failed to open file for read");
return -1;
int x = getw(fp);
if (feof(fp))
printf ("End of file reachedn");
else if (ferror(fp))
printf ("Error occurredn");
else
printf ("%dn", x);
return 0;
Output:
# ./a.out
65
You are getting -1 because getw() reads the next word from the stream (the size of a word is the size of an int and may vary from machine to machine) but in you case, when it try to read word from file it encounters EOF and the end-of-file indicator for the stream is set and getw() returns EOF. Note that EOF is a macro which expands to an integer constant expression with type int and an implementation dependent negative value but is very commonly -1.
You should use putw() to write the file, if you want to use getw() to read the file.
To show you the difference in file when using putc() and putw() to write the file:
When using putc():
putc('A',fp);
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 1B May 11 18:02 first.txt
^
|
size: 1 byte
When using putw():
putw('A', fp); // the first parameter type of putw() is int and the character will implicitly promoted to int
check the size of file:
# ls -lh first.txt
-rw-r--r-- 1 <owner> <group> 4B May 11 18:00 first.txt
^
|
size: 4 bytes
If using putw():
#include <stdio.h>
int main(void)
FILE *fp;
fp = fopen("first.txt", "w");
if (fp == NULL)
fprintf (stderr, "Failed to open file for write");
return -1;
putw('A', fp); // add the error handling for putw()
fclose(fp);
fp = fopen("first.txt", "r");
if (fp == NULL)
fprintf (stderr, "Failed to open file for read");
return -1;
int x = getw(fp);
if (feof(fp))
printf ("End of file reachedn");
else if (ferror(fp))
printf ("Error occurredn");
else
printf ("%dn", x);
return 0;
Output:
# ./a.out
65
answered May 11 at 13:12
H.S.H.S.
5,8041520
5,8041520
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f56090058%2fwhy-does-getw-return-1-when-trying-to-read-a-character%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
How did you even hear about
getw? This suggests something very very wrong with whatever material you're learning from...– R..
May 11 at 19:04