Hi im realtively new to c, I am trying to write a program that will read
in a text file and sort it, and then write the result back over the the text file.

So far im having trouble reading the file into a buffer. Now i was thinking i'd just read it into a 2d char Array. So to actually make the buffer i need the number of lines in the textfile which i wrote a method to get, but what has stumped me is how to actually create the buffer if its allready initalized. Plus im also thinking there is a much better way to do this then my approach.

Finally, textLine has to be defined in the numberoflinesinfile function.

Bob

10-26-2005

fatdunky

Ahh heh yeah whoops. Seeing as i dont use textLine in that loop (its just a count of the lines) i could just use a "" couldnt i?.

10-26-2005

caroundw5h

:eek: even with your code commented out, my compiler barfs.
i'm not sure if this is homework or not, but are you not supposed to use fread and fwrite. Seems to me it would make your life simpler. I would look into those, if your not supposed to use them, then forgive me.

As for setting aside a buffer. your code

Code:

char *fileBuffer[][];

is a 2 demensional arry of char *. Maybe its just me, but i would simply allocate a buffer as in

Code:

char buffer[BUFSIZ]

main also returns an int...but i'm sure someone else will berate you for that. I don't mean to be rude but i would read the chapter on files over before i have another go at this code. :eek:

10-26-2005

fatdunky

Umm nah its not homework. Im just learning c at my spare time at work. Yeah ive been using online tutorials ,none of them mentioned fread() fwrite() well not for text files they dont...
ill have a look into it thanks for the advice

10-26-2005

fatdunky

Oh and returning the int in the main. I started out with exit(1);, but for some reason my comipler would give a warning
Warning W8004 H:\C exercises\sort.c 39: 'linesoftxtinfile' is assigned a value that is never used in function main.

But when i return it doesnt. i think i saw someone do it somewhere on a online tut.

Plus im pretty sure it wouldnt work anyway because arr[integer] would grab a char. But i thought i might try. :)

10-27-2005

cwr

You need to #include <stdlib.h> for exit.

The line 32 warning is that it is looking for main to return a value, because main returns an int. You've made main return int implicitly, which is a deprecated construct, you should declare main as "int main...".

The line 70 error is because you're not passing strcmp two strings (char *).

10-27-2005

fatdunky

heh im back again. thanks for the help so far guys.

Ive had another crack at it i seem to have most of it sorted out execpt for one thing. Swapping the strings in the array. I can't really figure out how to do it. (because they can be a diffrent size, and i figure that could screw up the array if i swapped them) anyone have any ideas. I was thinking of making an array of pointers to strings but have no idea of how to create it in the program from the way i read in the file.

fread will return the number of bytes it reads on success so for your third argument use an unsigned int so you can check if it was read successfully, in this case again BUFSIZ, and your second argument just the sizeof(data type)

Plus im also thinking there is a much better way to do this then my approach.

This is just one of about three different approaches that I can think of to resolve your problem. I haven't tested it thoroughly. I'll let that up to you. Also, there is an excellent description of binary trees in (I believe) the FAQs area. It would behoove you to read those binary tree articles in order to understand the code.

Example to sort one file: Sort input.txt input.txt
This sorts one file the input.txt file.

Also, you'll have to flesh out the error checking such as checking for valid fopen's etc.

if((filePtr = fopen(fileName,"w")) != NULL)
{
WriteFileFromTree(BTRootOut,filePtr);
if(fclose(filePtr)==EOF)
{
perror("Sort");
exit(1);
}
}
else
{
fclose(filePtr); //Didnt catch close errors here because there is allready an open error.
perror("Sort"); // And code would be the same if there was error or no error
exit(1);
}
}

I suppose the code after the else block does get executed, but i just find your logic to do so round about, but hey if it works for you who am i to say. I would have done it a bit more straight forward