My C code working on Windows perfectly but isn't working on Ubuntu exactly [on hold]
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++
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A yesterday
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++
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A yesterday
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
2 days ago
I'd guess the input file has improper line endings (rņ(DOS) vs.n(Unix)).
– PerlDuck
2 days ago
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
2 days ago
@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
2 days ago
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
2 days ago
|
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++
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
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++
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited yesterday
Zanna
50k13131238
50k13131238
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 2 days ago
Kadir Saglam
61
61
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Kadir Saglam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
put on hold as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A yesterday
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.
put on hold as off-topic by Thomas, Kulfy, Charles Green, N0rbert, Soren A yesterday
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
2 days ago
I'd guess the input file has improper line endings (rņ(DOS) vs.n(Unix)).
– PerlDuck
2 days ago
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
2 days ago
@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
2 days ago
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
2 days ago
|
show 3 more comments
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
2 days ago
I'd guess the input file has improper line endings (rņ(DOS) vs.n(Unix)).
– PerlDuck
2 days ago
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
2 days ago
@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
2 days ago
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
2 days ago
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
2 days ago
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
2 days ago
I'd guess the input file has improper line endings (
rņ (DOS) vs. n (Unix)).– PerlDuck
2 days ago
I'd guess the input file has improper line endings (
rņ (DOS) vs. n (Unix)).– PerlDuck
2 days ago
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
2 days ago
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
2 days ago
@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
2 days ago
@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
2 days ago
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
2 days ago
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
2 days ago
|
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
2 days ago
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
2 days ago
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
2 days ago
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 2 days ago
Kristopher Ives
1,5791016
1,5791016
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
2 days ago
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
2 days ago
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
2 days ago
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
2 days ago
add a comment |
On windows output file exactly i want but on ubuntu there are newlines in some irrelevant places.
– Kadir Saglam
2 days ago
I'd guess the input file has improper line endings (
rņ(DOS) vs.n(Unix)).– PerlDuck
2 days ago
Should i use /r/n instead of n checking for newlines?
– Kadir Saglam
2 days ago
@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
2 days ago
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
2 days ago