strlen problems, returns the right length + 2
I have a weird bug.
I wrote a function that gets a file and returns the length of each line:
void readFile1(char *argv, int fileNumber,
char *array_of_lines[NUMBER_OF_LINES+1], int *currNumOfLines)
{
FILE* fp;
fp = fopen(argv[fileNumber], "r");
if (fp == NULL)
{
fprintf(stderr, MSG_ERROR_OPEN_FILE, argv[fileNumber]);
exit(EXIT_FAILURE);
}
char line[256];
while (fgets(line, sizeof(line), fp))
{
printf("n line contains : %s size is : %lun",line,strlen(line));
}
}
The function always prints the right number + 2,
For example if file.txt contains only one line with "AAAAA"
the function would print that the length is 7
instead of 5
,
line contains : AAAAA
size is : 7
Does someone know where is the bug?
c strlen
add a comment |
I have a weird bug.
I wrote a function that gets a file and returns the length of each line:
void readFile1(char *argv, int fileNumber,
char *array_of_lines[NUMBER_OF_LINES+1], int *currNumOfLines)
{
FILE* fp;
fp = fopen(argv[fileNumber], "r");
if (fp == NULL)
{
fprintf(stderr, MSG_ERROR_OPEN_FILE, argv[fileNumber]);
exit(EXIT_FAILURE);
}
char line[256];
while (fgets(line, sizeof(line), fp))
{
printf("n line contains : %s size is : %lun",line,strlen(line));
}
}
The function always prints the right number + 2,
For example if file.txt contains only one line with "AAAAA"
the function would print that the length is 7
instead of 5
,
line contains : AAAAA
size is : 7
Does someone know where is the bug?
c strlen
1
Can you check this one: C function (fgets) mitigation
– Sourav Ghosh
Nov 15 '18 at 12:35
4
The 7 is right there. You have WindowsLinefeed I assume. So your lineAAAAA
is technicallyAAAAArn
– Maximilian Ast
Nov 15 '18 at 12:37
2
Change"n line contains : %s size is : %lun"
to"n line contains : <%s> size is : %lun"
and run the program again. Things will become clearer.
– Jabberwocky
Nov 15 '18 at 12:40
1
What is the "right number" exactly? The number of printable characters?
– David Schwartz
Nov 15 '18 at 12:44
What is your platform?
– Jabberwocky
Nov 15 '18 at 12:47
add a comment |
I have a weird bug.
I wrote a function that gets a file and returns the length of each line:
void readFile1(char *argv, int fileNumber,
char *array_of_lines[NUMBER_OF_LINES+1], int *currNumOfLines)
{
FILE* fp;
fp = fopen(argv[fileNumber], "r");
if (fp == NULL)
{
fprintf(stderr, MSG_ERROR_OPEN_FILE, argv[fileNumber]);
exit(EXIT_FAILURE);
}
char line[256];
while (fgets(line, sizeof(line), fp))
{
printf("n line contains : %s size is : %lun",line,strlen(line));
}
}
The function always prints the right number + 2,
For example if file.txt contains only one line with "AAAAA"
the function would print that the length is 7
instead of 5
,
line contains : AAAAA
size is : 7
Does someone know where is the bug?
c strlen
I have a weird bug.
I wrote a function that gets a file and returns the length of each line:
void readFile1(char *argv, int fileNumber,
char *array_of_lines[NUMBER_OF_LINES+1], int *currNumOfLines)
{
FILE* fp;
fp = fopen(argv[fileNumber], "r");
if (fp == NULL)
{
fprintf(stderr, MSG_ERROR_OPEN_FILE, argv[fileNumber]);
exit(EXIT_FAILURE);
}
char line[256];
while (fgets(line, sizeof(line), fp))
{
printf("n line contains : %s size is : %lun",line,strlen(line));
}
}
The function always prints the right number + 2,
For example if file.txt contains only one line with "AAAAA"
the function would print that the length is 7
instead of 5
,
line contains : AAAAA
size is : 7
Does someone know where is the bug?
c strlen
c strlen
edited Nov 15 '18 at 14:32
jps
3,71361936
3,71361936
asked Nov 15 '18 at 12:33
JohnJohn
62
62
1
Can you check this one: C function (fgets) mitigation
– Sourav Ghosh
Nov 15 '18 at 12:35
4
The 7 is right there. You have WindowsLinefeed I assume. So your lineAAAAA
is technicallyAAAAArn
– Maximilian Ast
Nov 15 '18 at 12:37
2
Change"n line contains : %s size is : %lun"
to"n line contains : <%s> size is : %lun"
and run the program again. Things will become clearer.
– Jabberwocky
Nov 15 '18 at 12:40
1
What is the "right number" exactly? The number of printable characters?
– David Schwartz
Nov 15 '18 at 12:44
What is your platform?
– Jabberwocky
Nov 15 '18 at 12:47
add a comment |
1
Can you check this one: C function (fgets) mitigation
– Sourav Ghosh
Nov 15 '18 at 12:35
4
The 7 is right there. You have WindowsLinefeed I assume. So your lineAAAAA
is technicallyAAAAArn
– Maximilian Ast
Nov 15 '18 at 12:37
2
Change"n line contains : %s size is : %lun"
to"n line contains : <%s> size is : %lun"
and run the program again. Things will become clearer.
– Jabberwocky
Nov 15 '18 at 12:40
1
What is the "right number" exactly? The number of printable characters?
– David Schwartz
Nov 15 '18 at 12:44
What is your platform?
– Jabberwocky
Nov 15 '18 at 12:47
1
1
Can you check this one: C function (fgets) mitigation
– Sourav Ghosh
Nov 15 '18 at 12:35
Can you check this one: C function (fgets) mitigation
– Sourav Ghosh
Nov 15 '18 at 12:35
4
4
The 7 is right there. You have WindowsLinefeed I assume. So your line
AAAAA
is technically AAAAArn
– Maximilian Ast
Nov 15 '18 at 12:37
The 7 is right there. You have WindowsLinefeed I assume. So your line
AAAAA
is technically AAAAArn
– Maximilian Ast
Nov 15 '18 at 12:37
2
2
Change
"n line contains : %s size is : %lun"
to "n line contains : <%s> size is : %lun"
and run the program again. Things will become clearer.– Jabberwocky
Nov 15 '18 at 12:40
Change
"n line contains : %s size is : %lun"
to "n line contains : <%s> size is : %lun"
and run the program again. Things will become clearer.– Jabberwocky
Nov 15 '18 at 12:40
1
1
What is the "right number" exactly? The number of printable characters?
– David Schwartz
Nov 15 '18 at 12:44
What is the "right number" exactly? The number of printable characters?
– David Schwartz
Nov 15 '18 at 12:44
What is your platform?
– Jabberwocky
Nov 15 '18 at 12:47
What is your platform?
– Jabberwocky
Nov 15 '18 at 12:47
add a comment |
2 Answers
2
active
oldest
votes
Don't forget that fgets
leaves the newline in the buffer.
It seems you're reading a file created in Windows (where a newline is the two characters "rn"
) on a system where newline is only "n"
. Those two characters are also part of the string and will be counted by strlen
.
The reason I'm guessing you're reading a Windows-created file in a non-Windows system is because for files open in text-mode (the default) then the functions reading and writing strings will translate newlines from and to the operating-system dependent format.
For example on Windows, when writing plain "n"
it will be translated and actually written as "rn"
. When reading the opposite translation happens ("rn"
becomes "n"
).
On a system with plain "n"
line endings (like Linux or macOS), no translation is needed, and the "r"
part will be treated as any other character.
add a comment |
printf("n line contains : %s size is : %lun",line,strlen(line));
Here's a giveaway
Obtained output
line contains : AAAAA
size is : 7
Expected output
line contains : AAAAA size is : 7
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%2f53319619%2fstrlen-problems-returns-the-right-length-2%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Don't forget that fgets
leaves the newline in the buffer.
It seems you're reading a file created in Windows (where a newline is the two characters "rn"
) on a system where newline is only "n"
. Those two characters are also part of the string and will be counted by strlen
.
The reason I'm guessing you're reading a Windows-created file in a non-Windows system is because for files open in text-mode (the default) then the functions reading and writing strings will translate newlines from and to the operating-system dependent format.
For example on Windows, when writing plain "n"
it will be translated and actually written as "rn"
. When reading the opposite translation happens ("rn"
becomes "n"
).
On a system with plain "n"
line endings (like Linux or macOS), no translation is needed, and the "r"
part will be treated as any other character.
add a comment |
Don't forget that fgets
leaves the newline in the buffer.
It seems you're reading a file created in Windows (where a newline is the two characters "rn"
) on a system where newline is only "n"
. Those two characters are also part of the string and will be counted by strlen
.
The reason I'm guessing you're reading a Windows-created file in a non-Windows system is because for files open in text-mode (the default) then the functions reading and writing strings will translate newlines from and to the operating-system dependent format.
For example on Windows, when writing plain "n"
it will be translated and actually written as "rn"
. When reading the opposite translation happens ("rn"
becomes "n"
).
On a system with plain "n"
line endings (like Linux or macOS), no translation is needed, and the "r"
part will be treated as any other character.
add a comment |
Don't forget that fgets
leaves the newline in the buffer.
It seems you're reading a file created in Windows (where a newline is the two characters "rn"
) on a system where newline is only "n"
. Those two characters are also part of the string and will be counted by strlen
.
The reason I'm guessing you're reading a Windows-created file in a non-Windows system is because for files open in text-mode (the default) then the functions reading and writing strings will translate newlines from and to the operating-system dependent format.
For example on Windows, when writing plain "n"
it will be translated and actually written as "rn"
. When reading the opposite translation happens ("rn"
becomes "n"
).
On a system with plain "n"
line endings (like Linux or macOS), no translation is needed, and the "r"
part will be treated as any other character.
Don't forget that fgets
leaves the newline in the buffer.
It seems you're reading a file created in Windows (where a newline is the two characters "rn"
) on a system where newline is only "n"
. Those two characters are also part of the string and will be counted by strlen
.
The reason I'm guessing you're reading a Windows-created file in a non-Windows system is because for files open in text-mode (the default) then the functions reading and writing strings will translate newlines from and to the operating-system dependent format.
For example on Windows, when writing plain "n"
it will be translated and actually written as "rn"
. When reading the opposite translation happens ("rn"
becomes "n"
).
On a system with plain "n"
line endings (like Linux or macOS), no translation is needed, and the "r"
part will be treated as any other character.
edited Nov 15 '18 at 12:49
answered Nov 15 '18 at 12:38
Some programmer dudeSome programmer dude
302k25264424
302k25264424
add a comment |
add a comment |
printf("n line contains : %s size is : %lun",line,strlen(line));
Here's a giveaway
Obtained output
line contains : AAAAA
size is : 7
Expected output
line contains : AAAAA size is : 7
add a comment |
printf("n line contains : %s size is : %lun",line,strlen(line));
Here's a giveaway
Obtained output
line contains : AAAAA
size is : 7
Expected output
line contains : AAAAA size is : 7
add a comment |
printf("n line contains : %s size is : %lun",line,strlen(line));
Here's a giveaway
Obtained output
line contains : AAAAA
size is : 7
Expected output
line contains : AAAAA size is : 7
printf("n line contains : %s size is : %lun",line,strlen(line));
Here's a giveaway
Obtained output
line contains : AAAAA
size is : 7
Expected output
line contains : AAAAA size is : 7
answered Nov 15 '18 at 14:37
pmgpmg
84.3k998169
84.3k998169
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%2f53319619%2fstrlen-problems-returns-the-right-length-2%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
1
Can you check this one: C function (fgets) mitigation
– Sourav Ghosh
Nov 15 '18 at 12:35
4
The 7 is right there. You have WindowsLinefeed I assume. So your line
AAAAA
is technicallyAAAAArn
– Maximilian Ast
Nov 15 '18 at 12:37
2
Change
"n line contains : %s size is : %lun"
to"n line contains : <%s> size is : %lun"
and run the program again. Things will become clearer.– Jabberwocky
Nov 15 '18 at 12:40
1
What is the "right number" exactly? The number of printable characters?
– David Schwartz
Nov 15 '18 at 12:44
What is your platform?
– Jabberwocky
Nov 15 '18 at 12:47