Learning C, have a question about line separators

Psych

Senior member
Feb 3, 2004
324
0
0
I was making a small program to number the lines in a text file to print.

I am just starting to learn C, so don't flame me.
The program takes in a file and creates an output file with the lines numbered. Since if I was to print this, the letters must be stopped somewhere, so I used fgets() with the second buffer parameter. The program is also supposed to number the new line created by this method.

For example:

aaaaaaaaaaaaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbbbbb =

1 aaaaaaaaaaaaaaaaaaaaaaaaaaaa
2 bbbbbbbbbbbbbbbbbbbbbb

I try to check for the '\n' newline character on the last useful character in the string to determine whether I should put one in, but the check never turns up positive.

As I understand it,
char string[5] = "BLUE"
string[4] == '\0'
string[3] == E

Is that correct? If so, why isn't my program working?
 

Psych

Senior member
Feb 3, 2004
324
0
0
Well, I got this to work with strlen, but can anyone confirm my assumptions about the strings? Is:
char str[length] = something;
str[length - 1] == '\0'; // Binary 0 and is the last addressable char of the string
str[length - 2] == stuff; // Assuming that the string is completely filled up, this is the last useful character?
 

Smilin

Diamond Member
Mar 4, 2002
7,357
0
0
Originally posted by: Psych


As I understand it,
char string[5] = "BLUE"
string[4] == '\0'
string[3] == E

Is that correct?

It's been a loooong time for me but I believe that is correct. Numbering starts at 0 and the string is terminated with a NULL character.
 

kamper

Diamond Member
Mar 18, 2003
5,513
0
0
That's just about the deal. One thing to note is that your example is very convenient: there is one less character in your string than in your array. If you don't follow that rule things will change. For instance, if you make a shorter string then the null character will move up and the end of your array will be undefined. If you make the string longer it will just go on past. Remember that you can always address your array with a number that is out of bounds and nothing will go wrong until you actually mess something up (very unpredictable).

I've got a little suspicion about what's actually going on when you say:
char string[5] = "BLUE";
When you declare an inline string the compiler should be automatically giving you memory for you (on the stack?). Also, when you declare an array the compiler gives you memory for that too (on the stack). I'd bet that what you are doing is acutally allocating double the memory and just abandoning the array memory. Try this:
char string[5];
println("%p", string);
string = "BLUE";
println("%p", string);
That should be the same implementation. In case you're wondering, that should print out the memory location of the string variable. I think that you'd get different addresses for each one; not sure though. At any rate what you really should be doing is:
char * string = "BLUE";
That way you don't have to worry about declaring the right array size. You'll also learn about how arrays and pointers are the same thing.

Were you implying that you now have your program working? If not, post the source and we'll be glad to try and figure it out.