473,372 Members | 1,314 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

Help with changing char *string to int * string for sorting.

Hello,

If anybody could help me with this I would greatly appreciate it. Or at least
tell me why I get the output of this garbage:

49
49
10
49
52
10

I'm trying to sort integers from a text file. Sorry if my code is bad or way
off base. I am a newbie to the C Language & I am just learning Arrays &
pointers.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
void Outputdata(char filename[], char theString[]);
void Sortdata(int theString[], int n );

int main()
{
char theString[20000]="";
char filename[20];
int count;
int n=10;

int x[20000];

printf("\nPlease enter Filename: ");
gets(filename); /* user inputs filename */
Outputdata(filename, theString);
*x = atoi(theString);
Sortdata( x, n );
printf("\nAfter the sort, the contents are:\n");

for( count = 0; count < n; count++ )
printf("Count = %d = %.d\n", count, theString[ count ]);

}

void Sortdata( int String[], int n )

{
int index = 0;
int x = 0;
int temp;

while ( x < (n - 1) )
{
index = x + 1;
while ( index < n )
{
if( String[ x ] > String[ index ] )
{
temp = String[ x ];
String[ x ] = String[index];
String[index] = temp;
}
index++;
}
x++;
}
}
void Outputdata(char filename[], char theString[])
{

FILE *fp;

char buf[BUFSIZ+1];
fp = fopen (filename, "r"); /* file is 10 integers on separate lines */

while (fgets(buf, BUFSIZ, fp) != NULL)
{
strcat(theString, buf);
}

printf("\n%s", theString);

fclose(fp);

}

Nov 14 '05 #1
4 2514


Pokerkook wrote:
Hello,

If anybody could help me with this I would greatly appreciate it. Or at least
tell me why I get the output of this garbage:

49
49
10
49
52
10

I'm trying to sort integers from a text file. Sorry if my code is bad or way
off base. I am a newbie to the C Language & I am just learning Arrays &
pointers.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
void Outputdata(char filename[], char theString[]); This function should return a value which can be use
to determine whether or not the file was accessed.
void Sortdata(int theString[], int n );

int main()
{
char theString[20000]="";
using theString is a poor approach in parsing the file. char filename[20];
int count;
int n=10;

int x[20000];
Since the size of the array is unknown, why not dynamically
allocate space as you need it using function realloc?
You can define a data struct that will a member pointint to
the array and another member keeping a count. See the Example
below.
printf("\nPlease enter Filename: ");
gets(filename); /* user inputs filename */ Function gets very unsafe, use function fgets. Outputdata(filename, theString);
*x = atoi(theString);
Sortdata( x, n );
printf("\nAfter the sort, the contents are:\n");

for( count = 0; count < n; count++ )
printf("Count = %d = %.d\n", count, theString[ count ]);

}

void Sortdata( int String[], int n )

.........snip,,,,,,,,

You can use function qsort to sort the numbers.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct INTARRAY
{
int *num;
size_t count;
} INTARRAY;

int GetDataINTARRAY(const char *filename, INTARRAY *p);
void SortINTARRAY(INTARRAY *p, int (*cmp)(const void *, const void *));
void PrintINTARRAY(INTARRAY *p);
void FreeINTARRAY(INTARRAY *p);
int cmp(const void *v1, const void *v2);

int main(void)
{
char filename[64],*s;
INTARRAY myint = {NULL};

printf("\nPlease enter Filename: ");
fflush(stdout);
fgets(filename, sizeof filename,stdin);
if(s = strrchr(filename,'\n')) *s = '\0';
else while('\n' != getchar());
if(GetDataINTARRAY(filename, &myint))
{
puts("\tUnSorted");
PrintINTARRAY(&myint);
SortINTARRAY(&myint,cmp);
puts("\n\tSorted");
PrintINTARRAY(&myint);
}
else puts("File not Found or Memory Allocation Failure");
FreeINTARRAY(&myint);
return 0;
}

void SortINTARRAY(INTARRAY *p, int (*cmp)(const void *, const void *))
{
if(p->num)
qsort(p->num,(size_t)p->count,sizeof *p->num,cmp);
return;
}

int GetDataINTARRAY(const char *fname, INTARRAY *p)
{
FILE *fp;
int ibuf, *tmp;

if((fp = fopen (fname, "r")) == NULL) return 0;
while(1 == fscanf(fp," %d",&ibuf))
{
tmp = realloc(p->num,(p->count+1)*(sizeof *tmp));
if(tmp == NULL)
{
FreeINTARRAY(p);
return 0;
}
p->num = tmp;
p->num[p->count++] = ibuf;
}
fclose(fp);
return 1;
}

int cmp(const void *v1, const void *v2)
{
const int *i1 = v1;
const int *i2 = v2;

return (*i1<*i2)?-1:(*i1!=*i2);
}

void PrintINTARRAY(INTARRAY *p)
{
size_t i;

for(i = 0; i < p->count; i++)
printf("num[%u] = %d\n",i,p->num[i]);
return;
}

void FreeINTARRAY(INTARRAY *p)
{
free(p->num);
p->num = NULL;
p->count = 0;
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #2
Pokerkook <po*******@aol.com> wrote:
I'm trying to sort integers from a text file. Sorry if my code is bad or way
off base. I am a newbie to the C Language & I am just learning Arrays &
pointers. #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h> void Outputdata(char filename[], char theString[]);
void Sortdata(int theString[], int n ); int main()
{
char theString[20000]="";
char filename[20];
int count;
int n=10;

int x[20000]; printf("\nPlease enter Filename: ");
You need either a '\n' at the end of the string or call fflush(),
otherwise the string may not appear.
gets(filename); /* user inputs filename */
Never ever use gets(), this function is nothing than a desaster waiting
to happen. You can't check if the user inputs a file name with more
than 19 characters and if he does gets() will happily write past the
end of the buffer you set aside for 'filename'. Use fgets() instead.
Outputdata(filename, theString);
*x = atoi(theString);
It looks as if you have the impression that atoi() would convert all
numbers in the string into integers and store them successively in
the array 'x'. But that's not what atoi() does: it converts the
first number in the string it finds and returns this number. You
will have to chop up the string into the parts consisting of numbers
and apply atoi() to each of them. Things get a lot easier if you
use strtol() instead of atoi() since it a) lets you do error checking
and b) tells you where it left off, so you can use that knowledge
to figure out where to look for the next number. So you could do
something like (assuming there are only numbers and white-space in
the file and, beware, untested)

size_t i;
int x[ 20000 ];
char *startptr;
char *endptr;

for ( startptr = theString; i < 20000; i++ )
{
x[ i ] = ( int ) strtol( startptr, &endptr, 10 );
if ( startptr == endptr ) /* no number found in file anymore */
break;
startprt = endptr;
}

if ( i >= 20000 )
fprintf( stderr, "Stopped after converting 20000 numbers, "
"there could be more.\n" );
else
fprint( "Got %ld numbers\n", ( long ) ++i );

You could (and for a real program should) even build in more error
checking, i.e. for cases where numbers are too large to be repre-
sented as integers (or even as long integers).
Sortdata( x, n );
Why would you want to sort only the first 'n' numbers and not all
you read in?
printf("\nAfter the sort, the contents are:\n"); for( count = 0; count < n; count++ )
printf("Count = %d = %.d\n", count, theString[ count ]);
While the dot in "%.d" won't hurt it doesn't do anything useful.
And since you promised that main() would return an int you need
here
return 0;
or
return EXIT_SUCCESS;

(but you must include <stdlib.h> for the definition of EXIT_SUCCESS).
} void Sortdata( int String[], int n )
{
int index = 0;
int x = 0;
int temp; while ( x < (n - 1) )
{
index = x + 1;
while ( index < n )
{
if( String[ x ] > String[ index ] )
{
temp = String[ x ];
String[ x ] = String[index];
String[index] = temp;
}
index++;
}
x++;
}
}
Bubblesort - not very famous for it's speed. Why not use qsort() that
is already built-in into C?
void Outputdata(char filename[], char theString[])
{ FILE *fp; char buf[BUFSIZ+1];
fp = fopen (filename, "r"); /* file is 10 integers on separate lines */
And what is supposed to happen when the file does not exist or you
don't have permissions to open it, i.e. when fopen() returns NULL?
while (fgets(buf, BUFSIZ, fp) != NULL)
{
strcat(theString, buf);
} printf("\n%s", theString); fclose(fp); }


You should be more careful here. All this will probably work fine with
your short file, but when you try to use the program with a file that
is longer than the 20000 characters you set aside for 'theString'
suddenly all kind of weird problems will start to appear since you then
are going to write past the end of that buffer.

Once you got that program to work properly you should start to learn
about dynamical memory allocation. That will allow you to read in
files of all lengths (limited only by how much memory your system
is willing to give your program).
Regards, Jens
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
On Sat, 04 Dec 2004 16:35:48 +0000, Jens.Toerring wrote:
printf("\nPlease enter Filename: ");


You need either a '\n' at the end of the string or call fflush(),
otherwise the string may not appear.


You can always turn off the buffering on the standard output stream using
setvbuf(). It would save a lot of calls to fflush() every time you want to
print something to the controlling terminal.

--
Regards,
Gregory.
"Ding-a-Ding Dang, My Dang-a-Long Ling Long."
Nov 14 '05 #4
On Thu, 09 Dec 2004 11:36:56 +1100, Gregory Shearman wrote:
On Sat, 04 Dec 2004 16:35:48 +0000, Jens.Toerring wrote:
printf("\nPlease enter Filename: ");


You need either a '\n' at the end of the string or call fflush(),
otherwise the string may not appear.


You can always turn off the buffering on the standard output stream using
setvbuf(). It would save a lot of calls to fflush() every time you want to
print something to the controlling terminal.


However buffering is usually a GOOD THING. Try to avoid turning it off if
you can.

Sometimes a better strategy than flushing after you "print" is to flush
just before you input.

Lawrence
Nov 14 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: John | last post by:
I am new to c++ programming and i just want to make a program that takes a users name, then converts it to a hex string, then puts it together in a fixed order. An example output would be: ...
8
by: Foxy Kav | last post by:
Hi everyone, Im currently doing first year UNI, taking a programming course in C++, for one project i have to create a simple array manipulator... that i have done, but i cant figure out how to...
27
by: ruel loehr | last post by:
Hey guys, I am looking for some insight: Given two sorted arrays of integers A and B, where array B has enough extra room in it to hold the contents of both A and B. Merge array A and B...
1
by: Rahul | last post by:
Hi Everybody I have some problem in my script. please help me. This is script file. I have one *.inq file. I want run this script in XML files. But this script errors shows . If u want i am...
10
by: nk | last post by:
Hi, I'm a newbie. The code below sorts names entered. ---------------------------------------------------------------------------- #include <stdio.h> #include <conio.h> #include <string.h>...
0
by: georges the man | last post by:
The purpose: • Sorting and Searching • Numerical Analysis Design Specification You are to write a program called “StockAnalyser”. Your program will read a text file that contains historical...
5
by: Renato | last post by:
I have an array of pointers to class Shape. I create 4 items and display their values. shapes Shape *shapes; shapes = new Shape (p1); shapes = new Triangle (p1); shapes = new Polygon (p2);
1
by: Ahmed Yasser | last post by:
Hi all, i have a problem with the datagridview sorting, the problem is a bit complicated so i hope i can describe in the following steps: 1. i have a datagridview with two columns...
3
by: powerej | last post by:
im having trouble printing an array with duplicate characters, i can enter and print with single characters but when i try and enter and print duplicate ones it puts blanks in my output for a sorted...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.