My C code working on Windows perfectly but isn't working on Ubuntu exactly [closed]
Example lines from input file:
abc@gmail.com Andee SMITH 1234
ADAM abc@gmail.com Andeee 21654
Anderea abc@gmail.com SAMMY 3524654
abc@gmail.com Andi BROWN 1245
Andie abc@gmail.com KNOWY 2485
Andra abc@gmail.com BRUCE 52445
Andrea abc@gmail.com 246574 DENNIS
2154 Andreana abc@gmail.com CHASE
Andree 21524 SIERRRA abc@gmail.com
Andrei 154 MONDY abc@gmail.com
4564765 Andria MALLE abc@gmail.com
78 Andriana abc@gmail.com WALLS
579874 abc@gmail.com Andriette MOUNT
52445 abc@gmail.com Andromache FRASSER
5478645 abc@gmail.com Andy MCFLY
//This program about scanning names,surnames ext. from disordered txt file and creating new ordered txt file.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char id[50];
char name[50];
char lname[50];
char mail[50];
}Person;
int main(){
char str[250];
int count=0;
char c;
char previous;
int index =0;
int i,m,l;
int j,k=0;
Person person[300];
char str1[250];
char in[1];
char p;
Person temp;
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
c= fgetc(file);
if(c=='n'){
++count;
}
}
fclose(file);
//person *person= (person*)malloc(count+1*sizeof(person));
file = fopen( "hw4_disordered_people.txt" , "r");
while(fgets(str1,sizeof(str1),file)){
++index;
for (j = 0; str1[j] != 'n'; ++j)
{
if (str1[j] != ' ' && str1[j+1] != 'n')
{
str[k] = str1[j];
k++;
}
else if (k > 0 )
{
if (str1[j+1] == 'n')
{
str[k] = str1[j];
k++;
}
str[k] = '';
k=0;
if(str[0] >= '0' && str[0] <='9')
{
strcpy(person[index].id,str);
//printf("%s ",person[index].id);
strcpy(str,"");
}
else if (str[1] >= 'A' && str[1] <='Z')
{
if(strlen(person[index].lname) > 0){
//printf(" %s ",str);
strcat(person[index].lname," ");
strcat(person[index].lname,str);
strcpy(str,"");
}
else{
strcpy(person[index].lname,str);
//printf("%s ",person[index].lname);
strcpy(str,"");
}
}
else if ((str[1] >= 'a' && str[1] <='z') && (str[0] >= 'A' && str[0] <='Z'))
{
if(strlen(person[index].name) > 0){
//printf(" %s ",str);
strcat(person[index].name," ");
strcat(person[index].name,str);
strcpy(str,"");
}
else{
strcpy(person[index].name,str);
//printf("%s ",person[index].name);
strcpy(str,"");
}
}
else
{
strcpy(person[index].mail,str);
//printf("%s ",person[index].mail);
strcpy(str,"");
}
}
}
}
FILE *fp;
fp=fopen("OrderedList.txt","w");
for(i=1;i<=count;++i){
fprintf(fp,"%s %s %s %sn",person[i].name,person[i].lname,person[i].mail,person[i].id);
}
}
compiling c++
closed as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A Dec 28 '18 at 10:19
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Thomas, Kulfy, Charles Green, N0rbert, Soren A
If this question can be reworded to fit the rules in the help center, please edit the question.
|
show 3 more comments
Example lines from input file:
abc@gmail.com Andee SMITH 1234
ADAM abc@gmail.com Andeee 21654
Anderea abc@gmail.com SAMMY 3524654
abc@gmail.com Andi BROWN 1245
Andie abc@gmail.com KNOWY 2485
Andra abc@gmail.com BRUCE 52445
Andrea abc@gmail.com 246574 DENNIS
2154 Andreana abc@gmail.com CHASE
Andree 21524 SIERRRA abc@gmail.com
Andrei 154 MONDY abc@gmail.com
4564765 Andria MALLE abc@gmail.com
78 Andriana abc@gmail.com WALLS
579874 abc@gmail.com Andriette MOUNT
52445 abc@gmail.com Andromache FRASSER
5478645 abc@gmail.com Andy MCFLY
//This program about scanning names,surnames ext. from disordered txt file and creating new ordered txt file.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char id[50];
char name[50];
char lname[50];
char mail[50];
}Person;
int main(){
char str[250];
int count=0;
char c;
char previous;
int index =0;
int i,m,l;
int j,k=0;
Person person[300];
char str1[250];
char in[1];
char p;
Person temp;
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
c= fgetc(file);
if(c=='n'){
++count;
}
}
fclose(file);
//person *person= (person*)malloc(count+1*sizeof(person));
file = fopen( "hw4_disordered_people.txt" , "r");
while(fgets(str1,sizeof(str1),file)){
++index;
for (j = 0; str1[j] != 'n'; ++j)
{
if (str1[j] != ' ' && str1[j+1] != 'n')
{
str[k] = str1[j];
k++;
}
else if (k > 0 )
{
if (str1[j+1] == 'n')
{
str[k] = str1[j];
k++;
}
str[k] = '';
k=0;
if(str[0] >= '0' && str[0] <='9')
{
strcpy(person[index].id,str);
//printf("%s ",person[index].id);
strcpy(str,"");
}
else if (str[1] >= 'A' && str[1] <='Z')
{
if(strlen(person[index].lname) > 0){
//printf(" %s ",str);
strcat(person[index].lname," ");
strcat(person[index].lname,str);
strcpy(str,"");
}
else{
strcpy(person[index].lname,str);
//printf("%s ",person[index].lname);
strcpy(str,"");
}
}
else if ((str[1] >= 'a' && str[1] <='z') && (str[0] >= 'A' && str[0] <='Z'))
{
if(strlen(person[index].name) > 0){
//printf(" %s ",str);
strcat(person[index].name," ");
strcat(person[index].name,str);
strcpy(str,"");
}
else{
strcpy(person[index].name,str);
//printf("%s ",person[index].name);
strcpy(str,"");
}
}
else
{
strcpy(person[index].mail,str);
//printf("%s ",person[index].mail);
strcpy(str,"");
}
}
}
}
FILE *fp;
fp=fopen("OrderedList.txt","w");
for(i=1;i<=count;++i){
fprintf(fp,"%s %s %s %sn",person[i].name,person[i].lname,person[i].mail,person[i].id);
}
}
compiling c++
closed as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A Dec 28 '18 at 10:19
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Thomas, Kulfy, Charles Green, N0rbert, Soren A
If this question can be reworded to fit the rules in the help center, please edit the question.
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
Dec 27 '18 at 11:58
I'd guess the input file has improper line endings (rņ
(DOS) vs.n
(Unix)).
– PerlDuck
Dec 27 '18 at 12:00
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
Dec 27 '18 at 12:02
@karel I don't think this is suitable as duplicate, especially since OP's asking about C code and how it should work between OS's
– Sergiy Kolodyazhnyy
Dec 27 '18 at 12:31
1
While you are at it, you should also add what error you face. Currently the question only contains a C program and the title says it doesn't work.
– PerlDuck
Dec 27 '18 at 13:10
|
show 3 more comments
Example lines from input file:
abc@gmail.com Andee SMITH 1234
ADAM abc@gmail.com Andeee 21654
Anderea abc@gmail.com SAMMY 3524654
abc@gmail.com Andi BROWN 1245
Andie abc@gmail.com KNOWY 2485
Andra abc@gmail.com BRUCE 52445
Andrea abc@gmail.com 246574 DENNIS
2154 Andreana abc@gmail.com CHASE
Andree 21524 SIERRRA abc@gmail.com
Andrei 154 MONDY abc@gmail.com
4564765 Andria MALLE abc@gmail.com
78 Andriana abc@gmail.com WALLS
579874 abc@gmail.com Andriette MOUNT
52445 abc@gmail.com Andromache FRASSER
5478645 abc@gmail.com Andy MCFLY
//This program about scanning names,surnames ext. from disordered txt file and creating new ordered txt file.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char id[50];
char name[50];
char lname[50];
char mail[50];
}Person;
int main(){
char str[250];
int count=0;
char c;
char previous;
int index =0;
int i,m,l;
int j,k=0;
Person person[300];
char str1[250];
char in[1];
char p;
Person temp;
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
c= fgetc(file);
if(c=='n'){
++count;
}
}
fclose(file);
//person *person= (person*)malloc(count+1*sizeof(person));
file = fopen( "hw4_disordered_people.txt" , "r");
while(fgets(str1,sizeof(str1),file)){
++index;
for (j = 0; str1[j] != 'n'; ++j)
{
if (str1[j] != ' ' && str1[j+1] != 'n')
{
str[k] = str1[j];
k++;
}
else if (k > 0 )
{
if (str1[j+1] == 'n')
{
str[k] = str1[j];
k++;
}
str[k] = '';
k=0;
if(str[0] >= '0' && str[0] <='9')
{
strcpy(person[index].id,str);
//printf("%s ",person[index].id);
strcpy(str,"");
}
else if (str[1] >= 'A' && str[1] <='Z')
{
if(strlen(person[index].lname) > 0){
//printf(" %s ",str);
strcat(person[index].lname," ");
strcat(person[index].lname,str);
strcpy(str,"");
}
else{
strcpy(person[index].lname,str);
//printf("%s ",person[index].lname);
strcpy(str,"");
}
}
else if ((str[1] >= 'a' && str[1] <='z') && (str[0] >= 'A' && str[0] <='Z'))
{
if(strlen(person[index].name) > 0){
//printf(" %s ",str);
strcat(person[index].name," ");
strcat(person[index].name,str);
strcpy(str,"");
}
else{
strcpy(person[index].name,str);
//printf("%s ",person[index].name);
strcpy(str,"");
}
}
else
{
strcpy(person[index].mail,str);
//printf("%s ",person[index].mail);
strcpy(str,"");
}
}
}
}
FILE *fp;
fp=fopen("OrderedList.txt","w");
for(i=1;i<=count;++i){
fprintf(fp,"%s %s %s %sn",person[i].name,person[i].lname,person[i].mail,person[i].id);
}
}
compiling c++
Example lines from input file:
abc@gmail.com Andee SMITH 1234
ADAM abc@gmail.com Andeee 21654
Anderea abc@gmail.com SAMMY 3524654
abc@gmail.com Andi BROWN 1245
Andie abc@gmail.com KNOWY 2485
Andra abc@gmail.com BRUCE 52445
Andrea abc@gmail.com 246574 DENNIS
2154 Andreana abc@gmail.com CHASE
Andree 21524 SIERRRA abc@gmail.com
Andrei 154 MONDY abc@gmail.com
4564765 Andria MALLE abc@gmail.com
78 Andriana abc@gmail.com WALLS
579874 abc@gmail.com Andriette MOUNT
52445 abc@gmail.com Andromache FRASSER
5478645 abc@gmail.com Andy MCFLY
//This program about scanning names,surnames ext. from disordered txt file and creating new ordered txt file.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct{
char id[50];
char name[50];
char lname[50];
char mail[50];
}Person;
int main(){
char str[250];
int count=0;
char c;
char previous;
int index =0;
int i,m,l;
int j,k=0;
Person person[300];
char str1[250];
char in[1];
char p;
Person temp;
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
c= fgetc(file);
if(c=='n'){
++count;
}
}
fclose(file);
//person *person= (person*)malloc(count+1*sizeof(person));
file = fopen( "hw4_disordered_people.txt" , "r");
while(fgets(str1,sizeof(str1),file)){
++index;
for (j = 0; str1[j] != 'n'; ++j)
{
if (str1[j] != ' ' && str1[j+1] != 'n')
{
str[k] = str1[j];
k++;
}
else if (k > 0 )
{
if (str1[j+1] == 'n')
{
str[k] = str1[j];
k++;
}
str[k] = '';
k=0;
if(str[0] >= '0' && str[0] <='9')
{
strcpy(person[index].id,str);
//printf("%s ",person[index].id);
strcpy(str,"");
}
else if (str[1] >= 'A' && str[1] <='Z')
{
if(strlen(person[index].lname) > 0){
//printf(" %s ",str);
strcat(person[index].lname," ");
strcat(person[index].lname,str);
strcpy(str,"");
}
else{
strcpy(person[index].lname,str);
//printf("%s ",person[index].lname);
strcpy(str,"");
}
}
else if ((str[1] >= 'a' && str[1] <='z') && (str[0] >= 'A' && str[0] <='Z'))
{
if(strlen(person[index].name) > 0){
//printf(" %s ",str);
strcat(person[index].name," ");
strcat(person[index].name,str);
strcpy(str,"");
}
else{
strcpy(person[index].name,str);
//printf("%s ",person[index].name);
strcpy(str,"");
}
}
else
{
strcpy(person[index].mail,str);
//printf("%s ",person[index].mail);
strcpy(str,"");
}
}
}
}
FILE *fp;
fp=fopen("OrderedList.txt","w");
for(i=1;i<=count;++i){
fprintf(fp,"%s %s %s %sn",person[i].name,person[i].lname,person[i].mail,person[i].id);
}
}
compiling c++
compiling c++
edited Dec 28 '18 at 10:07
Zanna
50.3k13133241
50.3k13133241
asked Dec 27 '18 at 11:55
Kadir SaglamKadir Saglam
61
61
closed as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A Dec 28 '18 at 10:19
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Thomas, Kulfy, Charles Green, N0rbert, Soren A
If this question can be reworded to fit the rules in the help center, please edit the question.
closed as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A Dec 28 '18 at 10:19
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This is not about Ubuntu. Questions about other Linux distributions can be asked on Unix & Linux, those about Windows on Super User, those about Apple products on Ask Different and generic programming questions on Stack Overflow." – Thomas, Kulfy, Charles Green, N0rbert, Soren A
If this question can be reworded to fit the rules in the help center, please edit the question.
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
Dec 27 '18 at 11:58
I'd guess the input file has improper line endings (rņ
(DOS) vs.n
(Unix)).
– PerlDuck
Dec 27 '18 at 12:00
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
Dec 27 '18 at 12:02
@karel I don't think this is suitable as duplicate, especially since OP's asking about C code and how it should work between OS's
– Sergiy Kolodyazhnyy
Dec 27 '18 at 12:31
1
While you are at it, you should also add what error you face. Currently the question only contains a C program and the title says it doesn't work.
– PerlDuck
Dec 27 '18 at 13:10
|
show 3 more comments
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
Dec 27 '18 at 11:58
I'd guess the input file has improper line endings (rņ
(DOS) vs.n
(Unix)).
– PerlDuck
Dec 27 '18 at 12:00
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
Dec 27 '18 at 12:02
@karel I don't think this is suitable as duplicate, especially since OP's asking about C code and how it should work between OS's
– Sergiy Kolodyazhnyy
Dec 27 '18 at 12:31
1
While you are at it, you should also add what error you face. Currently the question only contains a C program and the title says it doesn't work.
– PerlDuck
Dec 27 '18 at 13:10
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
Dec 27 '18 at 11:58
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
Dec 27 '18 at 11:58
I'd guess the input file has improper line endings (
rņ
(DOS) vs. n
(Unix)).– PerlDuck
Dec 27 '18 at 12:00
I'd guess the input file has improper line endings (
rņ
(DOS) vs. n
(Unix)).– PerlDuck
Dec 27 '18 at 12:00
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
Dec 27 '18 at 12:02
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
Dec 27 '18 at 12:02
@karel I don't think this is suitable as duplicate, especially since OP's asking about C code and how it should work between OS's
– Sergiy Kolodyazhnyy
Dec 27 '18 at 12:31
@karel I don't think this is suitable as duplicate, especially since OP's asking about C code and how it should work between OS's
– Sergiy Kolodyazhnyy
Dec 27 '18 at 12:31
1
1
While you are at it, you should also add what error you face. Currently the question only contains a C program and the title says it doesn't work.
– PerlDuck
Dec 27 '18 at 13:10
While you are at it, you should also add what error you face. Currently the question only contains a C program and the title says it doesn't work.
– PerlDuck
Dec 27 '18 at 13:10
|
show 3 more comments
1 Answer
1
active
oldest
votes
Your program has many errors in it, here are a few:
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
Here you don't check if opening the file succeeded and you begin to make use of the uninitialized variable c
, which can technically have any value sitting in memory assigned to it. Even with the assumed value of 0
this is also still wrong since this assumes that a file is at least 1 byte large meaning this program may do strange things with an empty file passed to it.
while(c!=EOF){ // how many of struct
c = fgetc(file);
if(c=='n'){
++count;
}
}
The correct way to write this would be closer to:
while ((c = fgetc(file)) != EOF) {
++count;
}
It's also worth noting that this portion of the program can be replaced by using the wc -l
command, which counts how many lines exist in a file.
if (str1[j+1] == 'n')
There is no guarantee that j+1
is a valid index into this array.
char str[250];
There is no guarantee that a line in the file won't be 248 characters or longer (including the newline and null terminator)
Person person[300];
There is no guarantee that the input file will be 300 or less lines.
str[k] = '';
k
may now point outside the bounds of the character array, although the input needed to do it is a pretty specific edge case.
strcpy(person[index].id,str);
Buffer overflow if index
is out of range (300)
for(i=1;i<=count;++i){
This likely attempts to access data outside the bounds of the array. The fact you increment early in the loop and discard index 0 of the array is strange, but not technically an error, but the fact that you are willing to access count + 1
as a valid address is an error and if you were using malloc
without a fixed array size or processing a file with 300 inputs this would segmentation fault / crash the program.
You should compile your code with gcc -Wall
and you will likely get a lot of these warnings automatically.
If your course material allows it you should be using C++ with the Standard Template Library (STL) in which case this entire program would be a few lines of code that have strong guarantees that it won't have many of the memory errors presented here since it would use std::string
and std::istream
to make this less of an issue along with std::unordered_map
to make lookups faster.
This program for my C programming homework so I can't use STL. I will try use malloc instead of fixed arrays.Thanks for the advices.
– Kadir Saglam
Dec 27 '18 at 13:10
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your program has many errors in it, here are a few:
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
Here you don't check if opening the file succeeded and you begin to make use of the uninitialized variable c
, which can technically have any value sitting in memory assigned to it. Even with the assumed value of 0
this is also still wrong since this assumes that a file is at least 1 byte large meaning this program may do strange things with an empty file passed to it.
while(c!=EOF){ // how many of struct
c = fgetc(file);
if(c=='n'){
++count;
}
}
The correct way to write this would be closer to:
while ((c = fgetc(file)) != EOF) {
++count;
}
It's also worth noting that this portion of the program can be replaced by using the wc -l
command, which counts how many lines exist in a file.
if (str1[j+1] == 'n')
There is no guarantee that j+1
is a valid index into this array.
char str[250];
There is no guarantee that a line in the file won't be 248 characters or longer (including the newline and null terminator)
Person person[300];
There is no guarantee that the input file will be 300 or less lines.
str[k] = '';
k
may now point outside the bounds of the character array, although the input needed to do it is a pretty specific edge case.
strcpy(person[index].id,str);
Buffer overflow if index
is out of range (300)
for(i=1;i<=count;++i){
This likely attempts to access data outside the bounds of the array. The fact you increment early in the loop and discard index 0 of the array is strange, but not technically an error, but the fact that you are willing to access count + 1
as a valid address is an error and if you were using malloc
without a fixed array size or processing a file with 300 inputs this would segmentation fault / crash the program.
You should compile your code with gcc -Wall
and you will likely get a lot of these warnings automatically.
If your course material allows it you should be using C++ with the Standard Template Library (STL) in which case this entire program would be a few lines of code that have strong guarantees that it won't have many of the memory errors presented here since it would use std::string
and std::istream
to make this less of an issue along with std::unordered_map
to make lookups faster.
This program for my C programming homework so I can't use STL. I will try use malloc instead of fixed arrays.Thanks for the advices.
– Kadir Saglam
Dec 27 '18 at 13:10
add a comment |
Your program has many errors in it, here are a few:
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
Here you don't check if opening the file succeeded and you begin to make use of the uninitialized variable c
, which can technically have any value sitting in memory assigned to it. Even with the assumed value of 0
this is also still wrong since this assumes that a file is at least 1 byte large meaning this program may do strange things with an empty file passed to it.
while(c!=EOF){ // how many of struct
c = fgetc(file);
if(c=='n'){
++count;
}
}
The correct way to write this would be closer to:
while ((c = fgetc(file)) != EOF) {
++count;
}
It's also worth noting that this portion of the program can be replaced by using the wc -l
command, which counts how many lines exist in a file.
if (str1[j+1] == 'n')
There is no guarantee that j+1
is a valid index into this array.
char str[250];
There is no guarantee that a line in the file won't be 248 characters or longer (including the newline and null terminator)
Person person[300];
There is no guarantee that the input file will be 300 or less lines.
str[k] = '';
k
may now point outside the bounds of the character array, although the input needed to do it is a pretty specific edge case.
strcpy(person[index].id,str);
Buffer overflow if index
is out of range (300)
for(i=1;i<=count;++i){
This likely attempts to access data outside the bounds of the array. The fact you increment early in the loop and discard index 0 of the array is strange, but not technically an error, but the fact that you are willing to access count + 1
as a valid address is an error and if you were using malloc
without a fixed array size or processing a file with 300 inputs this would segmentation fault / crash the program.
You should compile your code with gcc -Wall
and you will likely get a lot of these warnings automatically.
If your course material allows it you should be using C++ with the Standard Template Library (STL) in which case this entire program would be a few lines of code that have strong guarantees that it won't have many of the memory errors presented here since it would use std::string
and std::istream
to make this less of an issue along with std::unordered_map
to make lookups faster.
This program for my C programming homework so I can't use STL. I will try use malloc instead of fixed arrays.Thanks for the advices.
– Kadir Saglam
Dec 27 '18 at 13:10
add a comment |
Your program has many errors in it, here are a few:
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
Here you don't check if opening the file succeeded and you begin to make use of the uninitialized variable c
, which can technically have any value sitting in memory assigned to it. Even with the assumed value of 0
this is also still wrong since this assumes that a file is at least 1 byte large meaning this program may do strange things with an empty file passed to it.
while(c!=EOF){ // how many of struct
c = fgetc(file);
if(c=='n'){
++count;
}
}
The correct way to write this would be closer to:
while ((c = fgetc(file)) != EOF) {
++count;
}
It's also worth noting that this portion of the program can be replaced by using the wc -l
command, which counts how many lines exist in a file.
if (str1[j+1] == 'n')
There is no guarantee that j+1
is a valid index into this array.
char str[250];
There is no guarantee that a line in the file won't be 248 characters or longer (including the newline and null terminator)
Person person[300];
There is no guarantee that the input file will be 300 or less lines.
str[k] = '';
k
may now point outside the bounds of the character array, although the input needed to do it is a pretty specific edge case.
strcpy(person[index].id,str);
Buffer overflow if index
is out of range (300)
for(i=1;i<=count;++i){
This likely attempts to access data outside the bounds of the array. The fact you increment early in the loop and discard index 0 of the array is strange, but not technically an error, but the fact that you are willing to access count + 1
as a valid address is an error and if you were using malloc
without a fixed array size or processing a file with 300 inputs this would segmentation fault / crash the program.
You should compile your code with gcc -Wall
and you will likely get a lot of these warnings automatically.
If your course material allows it you should be using C++ with the Standard Template Library (STL) in which case this entire program would be a few lines of code that have strong guarantees that it won't have many of the memory errors presented here since it would use std::string
and std::istream
to make this less of an issue along with std::unordered_map
to make lookups faster.
Your program has many errors in it, here are a few:
FILE *file= fopen("hw4_disordered_people.txt","r");
while(c!=EOF){ // how many of struct
Here you don't check if opening the file succeeded and you begin to make use of the uninitialized variable c
, which can technically have any value sitting in memory assigned to it. Even with the assumed value of 0
this is also still wrong since this assumes that a file is at least 1 byte large meaning this program may do strange things with an empty file passed to it.
while(c!=EOF){ // how many of struct
c = fgetc(file);
if(c=='n'){
++count;
}
}
The correct way to write this would be closer to:
while ((c = fgetc(file)) != EOF) {
++count;
}
It's also worth noting that this portion of the program can be replaced by using the wc -l
command, which counts how many lines exist in a file.
if (str1[j+1] == 'n')
There is no guarantee that j+1
is a valid index into this array.
char str[250];
There is no guarantee that a line in the file won't be 248 characters or longer (including the newline and null terminator)
Person person[300];
There is no guarantee that the input file will be 300 or less lines.
str[k] = '';
k
may now point outside the bounds of the character array, although the input needed to do it is a pretty specific edge case.
strcpy(person[index].id,str);
Buffer overflow if index
is out of range (300)
for(i=1;i<=count;++i){
This likely attempts to access data outside the bounds of the array. The fact you increment early in the loop and discard index 0 of the array is strange, but not technically an error, but the fact that you are willing to access count + 1
as a valid address is an error and if you were using malloc
without a fixed array size or processing a file with 300 inputs this would segmentation fault / crash the program.
You should compile your code with gcc -Wall
and you will likely get a lot of these warnings automatically.
If your course material allows it you should be using C++ with the Standard Template Library (STL) in which case this entire program would be a few lines of code that have strong guarantees that it won't have many of the memory errors presented here since it would use std::string
and std::istream
to make this less of an issue along with std::unordered_map
to make lookups faster.
answered Dec 27 '18 at 12:19
Kristopher IvesKristopher Ives
1,88011016
1,88011016
This program for my C programming homework so I can't use STL. I will try use malloc instead of fixed arrays.Thanks for the advices.
– Kadir Saglam
Dec 27 '18 at 13:10
add a comment |
This program for my C programming homework so I can't use STL. I will try use malloc instead of fixed arrays.Thanks for the advices.
– Kadir Saglam
Dec 27 '18 at 13:10
This program for my C programming homework so I can't use STL. I will try use malloc instead of fixed arrays.Thanks for the advices.
– Kadir Saglam
Dec 27 '18 at 13:10
This program for my C programming homework so I can't use STL. I will try use malloc instead of fixed arrays.Thanks for the advices.
– Kadir Saglam
Dec 27 '18 at 13:10
add a comment |
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
Dec 27 '18 at 11:58
I'd guess the input file has improper line endings (
rņ
(DOS) vs.n
(Unix)).– PerlDuck
Dec 27 '18 at 12:00
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
Dec 27 '18 at 12:02
@karel I don't think this is suitable as duplicate, especially since OP's asking about C code and how it should work between OS's
– Sergiy Kolodyazhnyy
Dec 27 '18 at 12:31
1
While you are at it, you should also add what error you face. Currently the question only contains a C program and the title says it doesn't work.
– PerlDuck
Dec 27 '18 at 13:10