I need HELP!!!

Z

zizo

I want to make a programme that would read 30 numbers from a file then
display it using bubble sort and then read another 30 numbers from
another file and sort them, and finally combine the two files and sort
them. I'm a begginner and i dont know how to read files into my
programme. please help me.
 
S

sjftan

i give u some relative function on file operate.

FILE * fp;
char ch;

fp = fopen("url/name","r");//open the file R

ch = fgetc(fp); // get a char from the fp;

fclose(fp); // close the file ;

i hope it can help u.
 
J

jynlix

//u can try this one
#include <stdio.h>
#define MAX 30
int main(int argc, char** argv)
{
FILE* pf; //declare a file pointer first
int iNums[MAX];
int i = 0;

if (argc != 2)
{
printf("Usage:%s filename\n", argv[0]);
return 1;
}

//open a file
if ((pf = fopen("in.txt", "r")) == NULL)
{
printf("Unable to open the file, pls check it!\n");
return 2;
}

//read some numbers from the file
while (fscanf(pf, "%d", &iNums) != EOF)
{
fprintf(stdout, "%d ", iNums[i++]);
}

//remember to close it
fclose(pf);
return 0;
}

i hope this is useful
 
W

Walter Roberson

jynlix said:
//u can try this one

Hmmm, C99.
#include <stdio.h>
#define MAX 30
int main(int argc, char** argv)
{
FILE* pf; //declare a file pointer first
int iNums[MAX];
int i = 0;
if (argc != 2)
{
printf("Usage:%s filename\n", argv[0]);
return 1;
}

You test the argument count here, but you do not use any of
the arguments. The Usage you indicate implies that a filename is
to be supplied, but you do not use the argument as a filename.
As the Usage message is wrong, it should be corrected. As you
never actually use the arguments and as it is pointless to do
the test you do, the test against argc should be omitted, and
probably argc and argv should be omitted from the prototype of main().

Also, when you are writing out an error message, you would
normally send it to stderr, not to stdout.
//open a file
if ((pf = fopen("in.txt", "r")) == NULL)
{
printf("Unable to open the file, pls check it!\n");

Again, error messages to stderr. It would usually be a good idea to
indicate which file is the problem, and to indicate a reason if
one can be deduced.
return 2;

The standard assigns definite meanings to returning 0 from main
and to returning 1 from main, but returning anything else from main
has implementation-defined results.
}

//read some numbers from the file
while (fscanf(pf, "%d", &iNums) != EOF)
{
fprintf(stdout, "%d ", iNums[i++]);


fprintf() is not -usually- used with stdout: -usually- printf() is
used instead.

Your code assumes that there is no more data in the file than was
originally indicated, and assumes that there is no problem with the
file. If there is a character in the file which does not form part
of an integer, then the fscanf() will return 0, and your code will
think a value has been read, and will try to print out the
uninitialized value from iNums... and then will loop back and
try to print out the next uninitialized location from iNums,
and so on, continuing onward until the operating system generates
a memory fault from attempting to access entries of iNum that lie
beyond the declared array size. If the operating system never notices,
then eventually i will overflow back to 0, but that just means it
will print the input values over again; the program might never
terminate.
}


//remember to close it
fclose(pf);
return 0;
}

i hope this is useful

I notice that you did not use any the properties of C99 that
ae not also in C89, with the exception of the comment style.
The portability of your code to those who still use C89 would
have been increased if you had used traditional comment style
instead of C99 comment style.
 
N

Nick Keighley

i give u some relative function on file operate.

FILE * fp;
char ch;

fp = fopen("url/name","r");//open the file R

ch = fgetc(fp); // get a char from the fp;

fclose(fp); // close the file ;

i hope it can help u.

1. leave context in your post. That is, quote what you are replying to

2. please post in standard english. The last line of your reply should
read "I hope it can help you" or "I hope this helps you".

3. you do not check for errors in your fopen() call


--
Nick Keighley

A ruby trembled. Two tourmaline nets failed to rectify the laser beam.
A diamond noted the error. Both the error and the correction went into
the general computer.
Corwainer Smith "The Dead Lady of Clown Town"
 
P

pete

Walter said:
The standard assigns definite meanings to returning 0 from main
and to returning 1 from main, but returning anything else from main
has implementation-defined results.

The standard doesn't assign any meaning to returning 1 from main.
 
K

Keith Thompson

Nick Keighley said:
1. leave context in your post. That is, quote what you are replying to

2. please post in standard english. The last line of your reply should
read "I hope it can help you" or "I hope this helps you".

3. you do not check for errors in your fopen() call

4. fgetc() returns int, not char.
 
W

Walter Roberson

fp = fopen("url/name","r");//open the file R
[/QUOTE]

The standard C fopen() function is not able to open URLs (or
URI's). [There is also no promise that directories exist or that
the directory delimeter is '/' .]
 
K

Keith Thompson

The standard C fopen() function is not able to open URLs (or
URI's). [There is also no promise that directories exist or that
the directory delimeter is '/' .][/QUOTE]

That's not necessarily true. The standard doesn't say what file names
represent; an implementation could plausibly allow URLs to be
meaningfully passed to fopen(). (I don't know of any that do so.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,176
Messages
2,570,948
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top