Why does this “read file” function I've created in C behave differently in Linux than it does in Windows?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am using this function to read in a text file that contains strings which are read in and inserted into an AVL tree. Everything works perfectly fine in Windows, however once I try to run it in Linux it produces entirely different results (I get a bunch of redundant nodes whose keys are simply blank spaces). Would anyone be able to explain why this might be the case?
node *read_file(char *list_name)
{
char array[255];
char *token = NULL;
node *found = NULL;
node *tree = NULL;
FILE *file = fopen(list_name, "r");
if (file == NULL)
{
printf("Could not open filen");
return NULL;
}
while (fgets(array, 255, file) != NULL)
{
token = strtok(array, " n");
while (token != NULL)
{
found = find_key(token, tree);
if (found == NULL)
{
tree = insert(token, tree);
}
else
{
found->frequency++;
}
token = strtok(NULL, " n");
}
}
fclose(file);
return tree;
}
c
|
show 10 more comments
I am using this function to read in a text file that contains strings which are read in and inserted into an AVL tree. Everything works perfectly fine in Windows, however once I try to run it in Linux it produces entirely different results (I get a bunch of redundant nodes whose keys are simply blank spaces). Would anyone be able to explain why this might be the case?
node *read_file(char *list_name)
{
char array[255];
char *token = NULL;
node *found = NULL;
node *tree = NULL;
FILE *file = fopen(list_name, "r");
if (file == NULL)
{
printf("Could not open filen");
return NULL;
}
while (fgets(array, 255, file) != NULL)
{
token = strtok(array, " n");
while (token != NULL)
{
found = find_key(token, tree);
if (found == NULL)
{
tree = insert(token, tree);
}
else
{
found->frequency++;
}
token = strtok(NULL, " n");
}
}
fclose(file);
return tree;
}
c
1
char array[255];
should bechar array[255 + 1];
– sturcotte06
Nov 23 '18 at 18:53
2
if the file came from Windows, trytoken = strtok(array, " rn");
addr
– xing
Nov 23 '18 at 18:55
1
@StephanLechner having read Windows text files in Linux, ther
is preserved and will be read. Windows will convertrn
ton
– xing
Nov 23 '18 at 18:58
1
@xing: Your solution worked, thank you! I created the original text file in Windows and then ported it in a tar file to Linux so that explains everything.
– wallZ
Nov 23 '18 at 19:43
1
@wallZ Because of the null char.
– sturcotte06
Nov 23 '18 at 19:46
|
show 10 more comments
I am using this function to read in a text file that contains strings which are read in and inserted into an AVL tree. Everything works perfectly fine in Windows, however once I try to run it in Linux it produces entirely different results (I get a bunch of redundant nodes whose keys are simply blank spaces). Would anyone be able to explain why this might be the case?
node *read_file(char *list_name)
{
char array[255];
char *token = NULL;
node *found = NULL;
node *tree = NULL;
FILE *file = fopen(list_name, "r");
if (file == NULL)
{
printf("Could not open filen");
return NULL;
}
while (fgets(array, 255, file) != NULL)
{
token = strtok(array, " n");
while (token != NULL)
{
found = find_key(token, tree);
if (found == NULL)
{
tree = insert(token, tree);
}
else
{
found->frequency++;
}
token = strtok(NULL, " n");
}
}
fclose(file);
return tree;
}
c
I am using this function to read in a text file that contains strings which are read in and inserted into an AVL tree. Everything works perfectly fine in Windows, however once I try to run it in Linux it produces entirely different results (I get a bunch of redundant nodes whose keys are simply blank spaces). Would anyone be able to explain why this might be the case?
node *read_file(char *list_name)
{
char array[255];
char *token = NULL;
node *found = NULL;
node *tree = NULL;
FILE *file = fopen(list_name, "r");
if (file == NULL)
{
printf("Could not open filen");
return NULL;
}
while (fgets(array, 255, file) != NULL)
{
token = strtok(array, " n");
while (token != NULL)
{
found = find_key(token, tree);
if (found == NULL)
{
tree = insert(token, tree);
}
else
{
found->frequency++;
}
token = strtok(NULL, " n");
}
}
fclose(file);
return tree;
}
c
c
asked Nov 23 '18 at 18:52
wallZwallZ
166
166
1
char array[255];
should bechar array[255 + 1];
– sturcotte06
Nov 23 '18 at 18:53
2
if the file came from Windows, trytoken = strtok(array, " rn");
addr
– xing
Nov 23 '18 at 18:55
1
@StephanLechner having read Windows text files in Linux, ther
is preserved and will be read. Windows will convertrn
ton
– xing
Nov 23 '18 at 18:58
1
@xing: Your solution worked, thank you! I created the original text file in Windows and then ported it in a tar file to Linux so that explains everything.
– wallZ
Nov 23 '18 at 19:43
1
@wallZ Because of the null char.
– sturcotte06
Nov 23 '18 at 19:46
|
show 10 more comments
1
char array[255];
should bechar array[255 + 1];
– sturcotte06
Nov 23 '18 at 18:53
2
if the file came from Windows, trytoken = strtok(array, " rn");
addr
– xing
Nov 23 '18 at 18:55
1
@StephanLechner having read Windows text files in Linux, ther
is preserved and will be read. Windows will convertrn
ton
– xing
Nov 23 '18 at 18:58
1
@xing: Your solution worked, thank you! I created the original text file in Windows and then ported it in a tar file to Linux so that explains everything.
– wallZ
Nov 23 '18 at 19:43
1
@wallZ Because of the null char.
– sturcotte06
Nov 23 '18 at 19:46
1
1
char array[255];
should be char array[255 + 1];
– sturcotte06
Nov 23 '18 at 18:53
char array[255];
should be char array[255 + 1];
– sturcotte06
Nov 23 '18 at 18:53
2
2
if the file came from Windows, try
token = strtok(array, " rn");
add r
Nov 23 '18 at 18:55
if the file came from Windows, try
token = strtok(array, " rn");
add r
Nov 23 '18 at 18:55
1
1
@StephanLechner having read Windows text files in Linux, the
r
is preserved and will be read. Windows will convert rn
to n
Nov 23 '18 at 18:58
@StephanLechner having read Windows text files in Linux, the
r
is preserved and will be read. Windows will convert rn
to n
Nov 23 '18 at 18:58
1
1
@xing: Your solution worked, thank you! I created the original text file in Windows and then ported it in a tar file to Linux so that explains everything.
– wallZ
Nov 23 '18 at 19:43
@xing: Your solution worked, thank you! I created the original text file in Windows and then ported it in a tar file to Linux so that explains everything.
– wallZ
Nov 23 '18 at 19:43
1
1
@wallZ Because of the null char.
– sturcotte06
Nov 23 '18 at 19:46
@wallZ Because of the null char.
– sturcotte06
Nov 23 '18 at 19:46
|
show 10 more comments
1 Answer
1
active
oldest
votes
If you have issues using stdio
in windows respect unix (or linux) just use always b
specifier to fopen(3)
call. Line terminators in unix consist on a single n
char, while in windows they are composed of a sequence of rn
characters. The solution adopted by the windows port consist in allowing you to specify "rt"
, instead of "r"
to the fopen(3)
call, so the r
are filtered out before passing them to the calling code. Probably your problem will be solved by just using "rt"
instead of "r"
(Posix spec allows this flag to be used in unix, but ignores it, so using it always is no harmful) There's another "b"
specifier, that allows you to consider the file a binary file (so the transformation of eliminating all the r
chars isn't done. This is mainly mean for binary files.
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%2f53451704%2fwhy-does-this-read-file-function-ive-created-in-c-behave-differently-in-linux%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you have issues using stdio
in windows respect unix (or linux) just use always b
specifier to fopen(3)
call. Line terminators in unix consist on a single n
char, while in windows they are composed of a sequence of rn
characters. The solution adopted by the windows port consist in allowing you to specify "rt"
, instead of "r"
to the fopen(3)
call, so the r
are filtered out before passing them to the calling code. Probably your problem will be solved by just using "rt"
instead of "r"
(Posix spec allows this flag to be used in unix, but ignores it, so using it always is no harmful) There's another "b"
specifier, that allows you to consider the file a binary file (so the transformation of eliminating all the r
chars isn't done. This is mainly mean for binary files.
add a comment |
If you have issues using stdio
in windows respect unix (or linux) just use always b
specifier to fopen(3)
call. Line terminators in unix consist on a single n
char, while in windows they are composed of a sequence of rn
characters. The solution adopted by the windows port consist in allowing you to specify "rt"
, instead of "r"
to the fopen(3)
call, so the r
are filtered out before passing them to the calling code. Probably your problem will be solved by just using "rt"
instead of "r"
(Posix spec allows this flag to be used in unix, but ignores it, so using it always is no harmful) There's another "b"
specifier, that allows you to consider the file a binary file (so the transformation of eliminating all the r
chars isn't done. This is mainly mean for binary files.
add a comment |
If you have issues using stdio
in windows respect unix (or linux) just use always b
specifier to fopen(3)
call. Line terminators in unix consist on a single n
char, while in windows they are composed of a sequence of rn
characters. The solution adopted by the windows port consist in allowing you to specify "rt"
, instead of "r"
to the fopen(3)
call, so the r
are filtered out before passing them to the calling code. Probably your problem will be solved by just using "rt"
instead of "r"
(Posix spec allows this flag to be used in unix, but ignores it, so using it always is no harmful) There's another "b"
specifier, that allows you to consider the file a binary file (so the transformation of eliminating all the r
chars isn't done. This is mainly mean for binary files.
If you have issues using stdio
in windows respect unix (or linux) just use always b
specifier to fopen(3)
call. Line terminators in unix consist on a single n
char, while in windows they are composed of a sequence of rn
characters. The solution adopted by the windows port consist in allowing you to specify "rt"
, instead of "r"
to the fopen(3)
call, so the r
are filtered out before passing them to the calling code. Probably your problem will be solved by just using "rt"
instead of "r"
(Posix spec allows this flag to be used in unix, but ignores it, so using it always is no harmful) There's another "b"
specifier, that allows you to consider the file a binary file (so the transformation of eliminating all the r
chars isn't done. This is mainly mean for binary files.
edited Nov 30 '18 at 13:02
answered Nov 30 '18 at 12:39
Luis ColoradoLuis Colorado
4,5451718
4,5451718
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%2f53451704%2fwhy-does-this-read-file-function-ive-created-in-c-behave-differently-in-linux%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
char array[255];
should bechar array[255 + 1];
– sturcotte06
Nov 23 '18 at 18:53
2
if the file came from Windows, try
token = strtok(array, " rn");
addr
– xing
Nov 23 '18 at 18:55
1
@StephanLechner having read Windows text files in Linux, the
r
is preserved and will be read. Windows will convertrn
ton
– xing
Nov 23 '18 at 18:58
1
@xing: Your solution worked, thank you! I created the original text file in Windows and then ported it in a tar file to Linux so that explains everything.
– wallZ
Nov 23 '18 at 19:43
1
@wallZ Because of the null char.
– sturcotte06
Nov 23 '18 at 19:46