Below is my code. Qsort is not working. What is wrong?
************************************************** **********************
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#define MAX 10
int compare( const void *arg1, const void *arg2 );
typedef struct
{
char s_line[20];
} lines_to_sort_t;
int main (int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[20];
lines_to_sort_t table[MAX];
while ( (c = getopt(argc, argv, "r") ) != EOF )
{
switch (c)
{
case 'r':
printf("aaa \n");
break;
}
}
int i=0;
while (optind < argc)
{
fp = fopen(argv[optind], "r");
while ( fgets(text_ln, sizeof(text_ln), fp) != NULL)
{
strcpy(table[i].s_line, text_ln);
i++;
}
optind++;
}
qsort(table, sizeof table / sizeof table[0], sizeof table[0],
compare);
i = 0;
while (i < 3 )
{
printf( "%d: %s",i, table[i].s_line );
i++;
}
}
int compare( const void *arg1, const void *arg2 )
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);
} 6 2250 za******@gmail.com wrote:
Below is my code. Qsort is not working. What is wrong?
[...]
Hard to tell, since you didn't describe the way in
which your code is "not working." What did you expect
it to do, what did it do differently, and in what way
is that behavior "not working?"
Still, one thing leaps out: You fill the first `i'
elements of table[], but then you sort all `MAX' of them.
MAX is 10, so if `i' is (for example) 7 there will be
three trailing elements of table[] that have never been
given any values. They "contain garbage" (or worse), and
when you try to sort them along with the 7 valid entries
there's no telling what might happen.
There may be other problems in your code, too -- but
it's loaded up with non-Standard headers and functions that
I don't have the patience to study. Fix the known problem
and see if things become more understandable -- and if they
don't, good luck!
--
Eric Sosman es*****@acm-dot-org.invalid
On 21 Mrz., 02:35, Eric Sosman <esos...@acm-dot-org.invalidwrote:
zavno...@gmail.com wrote:
Below is my code. Qsort is not working. What is wrong?
[...]
Hard to tell, since you didn't describe the way in
which your code is "not working." What did you expect
it to do, what did it do differently, and in what way
is that behavior "not working?"
Still, one thing leaps out: You fill the first `i'
elements of table[], but then you sort all `MAX' of them.
MAX is 10, so if `i' is (for example) 7 there will be
three trailing elements of table[] that have never been
given any values. They "contain garbage" (or worse), and
when you try to sort them along with the 7 valid entries
there's no telling what might happen.
There may be other problems in your code, too -- but
it's loaded up with non-Standard headers and functions that
I don't have the patience to study. Fix the known problem
and see if things become more understandable -- and if they
don't, good luck!
--
Eric Sosman
esos...@acm-dot-org.invalid
Thank You Eric.
Problem was MAX number of elements. When I change MAX number to
correspond with number of added elements it works, but when this
number differs I have a problem.
How could I fix this?
Adnan Selimovic.
/*
Some clues found below.
I made things a bit bigger just so I wouldn't have to worry about
the string size of items in files I had laying around.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define MAX 100
int compare(const void *arg1, const void *arg2);
typedef struct {
char s_line[80];
} lines_to_sort_t;
int main(int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[80];
int i = 0;
lines_to_sort_t table[MAX];
if (argc 1 && argv[1]) {
fp = fopen(argv[1], "r");
if (fp) {
while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
strcpy(table[i++].s_line, text_ln);
if (i >= MAX) break;
}
qsort(table, i, sizeof table[0], compare);
i = 0;
while (i < 3) {
printf("%d: %s", i, table[i].s_line);
i++;
}
}
}
return 0;
}
int compare(const void *arg1, const void *arg2)
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);
}
/*
C:\tmp>type l.txt
key_column_usage
sql_languages
catalogs
procedure_columns
procedure_parameters
procedures
check_constraints
check_constraints_by_table
foreign_keys
schemata
referential_constraints
assertions
indexes
usage_privileges
collations
table_constraints
table_privileges
table_statistics
tables
tables_info
primary_keys
dbinfoliterals
translations
character_sets
trustee
constraint_column_usage
constraint_table_usage
statistics
column_domain_usage
column_privileges
columns
provider_types
view_column_usage
view_table_usage
views
C:\tmp>foo l.txt
0: assertions
1: catalogs
2: character_sets
*/
On 21 Mrz., 02:50, "user923005" <dcor...@connx.comwrote:
/*
Some clues found below.
I made things a bit bigger just so I wouldn't have to worry about
the string size of items in files I had laying around.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define MAX 100
int compare(const void *arg1, const void *arg2);
typedef struct {
char s_line[80];
} lines_to_sort_t;
int main(int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[80];
int i = 0;
lines_to_sort_t table[MAX];
if (argc 1 && argv[1]) {
fp = fopen(argv[1], "r");
if (fp) {
while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
strcpy(table[i++].s_line, text_ln);
if (i >= MAX) break;
}
qsort(table, i, sizeof table[0], compare);
i = 0;
while (i < 3) {
printf("%d: %s", i, table[i].s_line);
i++;
}
}
}
return 0;}
int compare(const void *arg1, const void *arg2)
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);}
/*
C:\tmp>type l.txt
key_column_usage
sql_languages
catalogs
procedure_columns
procedure_parameters
procedures
check_constraints
check_constraints_by_table
foreign_keys
schemata
referential_constraints
assertions
indexes
usage_privileges
collations
table_constraints
table_privileges
table_statistics
tables
tables_info
primary_keys
dbinfoliterals
translations
character_sets
trustee
constraint_column_usage
constraint_table_usage
statistics
column_domain_usage
column_privileges
columns
provider_types
view_column_usage
view_table_usage
views
C:\tmp>foo l.txt
0: assertions
1: catalogs
2: character_sets
*/
Thanx,
Magical "i" solved the problem.
On Mar 20, 6:59 pm, zavno...@gmail.com wrote:
On 21 Mrz., 02:50, "user923005" <dcor...@connx.comwrote:
/*
Some clues found below.
I made things a bit bigger just so I wouldn't have to worry about
the string size of items in files I had laying around.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define MAX 100
int compare(const void *arg1, const void *arg2);
typedef struct {
char s_line[80];
} lines_to_sort_t;
int main(int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[80];
int i = 0;
lines_to_sort_t table[MAX];
if (argc 1 && argv[1]) {
fp = fopen(argv[1], "r");
if (fp) {
while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
strcpy(table[i++].s_line, text_ln);
if (i >= MAX) break;
}
qsort(table, i, sizeof table[0], compare);
i = 0;
while (i < 3) {
printf("%d: %s", i, table[i].s_line);
i++;
}
}
}
return 0;}
int compare(const void *arg1, const void *arg2)
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);}
/*
C:\tmp>type l.txt
key_column_usage
sql_languages
catalogs
procedure_columns
procedure_parameters
procedures
check_constraints
check_constraints_by_table
foreign_keys
schemata
referential_constraints
assertions
indexes
usage_privileges
collations
table_constraints
table_privileges
table_statistics
tables
tables_info
primary_keys
dbinfoliterals
translations
character_sets
trustee
constraint_column_usage
constraint_table_usage
statistics
column_domain_usage
column_privileges
columns
provider_types
view_column_usage
view_table_usage
views
C:\tmp>foo l.txt
0: assertions
1: catalogs
2: character_sets
*/
Thanx,
Magical "i" solved the problem.- Hide quoted text -
There were some other, equally serious problems I fixed as well.
Did you notice (for instance) the check to see if the file pointer
existed before using it?
On 21 Mrz., 03:03, "user923005" <dcor...@connx.comwrote:
On Mar 20, 6:59 pm, zavno...@gmail.com wrote:
On 21 Mrz., 02:50, "user923005" <dcor...@connx.comwrote:
/*
Some clues found below.
I made things a bit bigger just so I wouldn't have to worry about
the string size of items in files I had laying around.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#define MAX 100
int compare(const void *arg1, const void *arg2);
typedef struct {
char s_line[80];
} lines_to_sort_t;
int main(int argc, char **argv)
{
int c;
FILE *fp;
char text_ln[80];
int i = 0;
lines_to_sort_t table[MAX];
if (argc 1 && argv[1]) {
fp = fopen(argv[1], "r");
if (fp) {
while (fgets(text_ln, sizeof(text_ln), fp) != NULL) {
strcpy(table[i++].s_line, text_ln);
if (i >= MAX) break;
}
qsort(table, i, sizeof table[0], compare);
i = 0;
while (i < 3) {
printf("%d: %s", i, table[i].s_line);
i++;
}
}
}
return 0;}
int compare(const void *arg1, const void *arg2)
{
const lines_to_sort_t *p1 = arg1;
const lines_to_sort_t *p2 = arg2;
return strcmp(p1->s_line, p2->s_line);}
/*
C:\tmp>type l.txt
key_column_usage
sql_languages
catalogs
procedure_columns
procedure_parameters
procedures
check_constraints
check_constraints_by_table
foreign_keys
schemata
referential_constraints
assertions
indexes
usage_privileges
collations
table_constraints
table_privileges
table_statistics
tables
tables_info
primary_keys
dbinfoliterals
translations
character_sets
trustee
constraint_column_usage
constraint_table_usage
statistics
column_domain_usage
column_privileges
columns
provider_types
view_column_usage
view_table_usage
views
C:\tmp>foo l.txt
0: assertions
1: catalogs
2: character_sets
*/
Thanx,
Magical "i" solved the problem.- Hide quoted text -
There were some other, equally serious problems I fixed as well.
Did you notice (for instance) the check to see if the file pointer
existed before using it?
Yes, I noticed. Thanx.
Code is only a draft. Final version will be more complex. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alex Vinokur |
last post by:
===================================
------------- Sorting -------------
Comparative performance measurement
===================================
Testsuite : Comparing Function Objects to...
|
by: SilverWolf |
last post by:
I need some help with sorting and shuffling array of strings. I can't
seem to get qsort working, and I don't even know how to start to shuffle
the array. Here is what I have for now:
#include...
|
by: Excluded_Middle |
last post by:
Suppose I have a struct
typdef struct foo
{
int age;
char *name;
}foo;
now I made a list of foo using
|
by: yatindran |
last post by:
hai
this is my 2d array.
int a = {
{5,2,20,1,30,10},
{23,15,7,9,11,3},
{40,50,34,24,14,4},
{9,10,11,12,13,14},
{31,4,18,8,27,17},
{44,32,13,19,41,19},
{1,2,3,4,5,6},
|
by: Bailey.W87 |
last post by:
my professor give me this assignment. Sort the R's B's and W's in an
array. for example, the user enter:
R B W W B B R W W R R W R B W
i need to swap the characters in the array and arrange it...
|
by: DKC |
last post by:
Hi,
Using VB.NET.
I have a datagrid having a strongly typed array of objects as its data
source.
The data from the array of objects is displayed by means of a table style,
which is fine, but...
|
by: Allie |
last post by:
How would I go about sorting this structure by title?
typedef struct {
char author;
char title;
char code;
int hold;
int loan;
} LIBRARY;
|
by: rushik |
last post by:
Hello all,
I am using structure in my program, and my aim is to sort this
structure based on some optimized sorting algo.
structure is
struct data
{
int account;
|
by: abracadabra |
last post by:
I am reading an old book - Programming Pearls 2nd edition recently. It
says, "Even though the general C++ program uses 50 times the memory
and CPU time of the specialized C program, it requires...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |