[Blabber] C Programming

Peter Shenkin shenkin at gmail.com
Tue Aug 5 12:57:24 UTC 2014


JC,

You corrected my comment on fgets(), and though I could swear I looked
at the man page yesterday to double-check before posting, you are
right.

If you specify 50 as the 2nd arg, it will read at most 49 chars from
the file, leaving room for the ascii NUL. So if the variable you are
reading into is 50 chars in length, you should be OK. (I had said it
should be 51, to make space for NUL.)

So Matt, this part of your code should be OK.

-P.

On Mon, Aug 4, 2014 at 10:57 PM, Connor Connor
<john.theman.connor at gmail.com> wrote:
> Hi Mathew,
>
> It looks like you already got a lot of good advice, but I thought I would
> mention:
> int maxline = (fgets(line, 50, fpInput));
>
> What this is doing is reading 1 line or at most 49 characters into the line
> buffer, which is what you want.  The fgets function also returns a pointer
> to the line buffer, which you are assigning to a variable of type integer.
> The name of the variable is maxline, which makes me think that you want the
> value to be the number that is written in the first line of your input file.
>
> In a way it makes sense, fgets returns a pointer to the line buffer, the
> line buffer contains a UTF-8 string denoting the number of lines in the
> file, and this string is then assigned to an integer variable.  It does
> *seem* that this variable should contain the number of lines.  However that
> is not what is happening, for a number of reasons. The value that maxline
> *actually* has is the address of line buffer, not the value.  Furthermore,
> even if the contents of the buffer would fit into an integer, the UTF-8
> representation of the number that you are reading from the file is not in
> same format that your computer uses to represent integers.
>
> The file you are reading is "encoded" and if you want to turn the UTF-8
> string format of a number into its integer equivalent, you will have to do a
> little more work:
> fgets(line, 50, fpInput);  // No need to assign the return value to
> anything.
> int maxline = atoi(line);
>
> You will also need to include "stdlib.h" at the top of your source file
> (although it may work fine and just complain a little if you don't).
>
> atoi stands for "ASCII to Integer", and (you guessed it) converts an ascii
> string into an integer.  This works for UTF-8 encoded strings of digits as
> well as ascii encoded strings of digits, because in this case the bytes turn
> out to be same.
>
> I hope that helps.  Good luck on your assignment.
>
> --JC
>
>
> On Mon, Aug 4, 2014 at 10:09 PM, Robert Diamond <rmd6502 at gmail.com> wrote:
>>
>> Yeah, this is one of the big problems with c - there's no built in string
>> object - I think you're confusing chars (a single character) with strings
>> (an array of chars).  Your file is essentially an array of arrays of chars
>> (more confused yet?)
>>
>> For FILE *'s you can use
>> while (!feof(fpInput)) {
>>    ...
>> }
>> to determine when you've hit the end of a file.
>> Also, Peter's comment about null terminators is important.
>> Robby's comment about strlen is very useful too - to determine
>> palindromity of a line you only have to go halfway across the line :-)
>>
>>
>> On Mon, Aug 4, 2014 at 10:04 PM, Robby O'Connor <robby.oconnor at gmail.com>
>> wrote:
>>>
>>> I ran it through gdb...it printed fine in gdb but otherwise segfaulted
>>>
>>> --Rob
>>> Sent from my phone...excuse any typos please!
>>>
>>> On Aug 4, 2014 9:54 PM, "Peter Shenkin" <shenkin at gmail.com> wrote:
>>>>
>>>> You can put in fprintf( stderr, ....) in various places to see where
>>>> the segv is coming from. The last line printed tells you that the segv
>>>> came after that line.
>>>>
>>>> I see you have:
>>>>
>>>> int maxline = (fgets(line, 50, fpInput));
>>>>
>>>> However, line is 50 chars long. Since fgets() appends an ASCII NUL
>>>> after reading up to 50 chars, you should declare maxline to be 51
>>>> chars long if any of your lines actually has 50 chars. This might not
>>>> be the actual cause of the segv, but it's certainly a blunder.
>>>>
>>>> -P.
>>>>
>>>> On Mon, Aug 4, 2014 at 8:15 PM, Matthew Duepner
>>>> <matthewduepner14 at gmail.com> wrote:
>>>> > I'm still very confused
>>>> >
>>>> > On Aug 4, 2014 7:21 PM, "Matthew Duepner" <matthewduepner14 at gmail.com>
>>>> > wrote:
>>>> >>
>>>> >> The first line of the input file gives the number of lines in that
>>>> >> file.
>>>> >>
>>>> >> On Aug 4, 2014 7:11 PM, "Guan Yang" <guan at yang.dk> wrote:
>>>> >>>
>>>> >>> You should include the full output from your compiler, which would
>>>> >>> have
>>>> >>> line numbers for that message, and also indicate whether it's a
>>>> >>> warning or
>>>> >>> an error. (When I try to compile your file, I get warnings only, but
>>>> >>> you may
>>>> >>> have -Werror on.)
>>>> >>>
>>>> >>> The first error is on your call to fgets. fgets returns char *, but
>>>> >>> you
>>>> >>> are assigning its value to an int. fgets returns a pointer to the
>>>> >>> string on
>>>> >>> success. I don't know what you expected it to return.
>>>> >>>
>>>> >>> Same thing with the warning on line 22. You're passing the return
>>>> >>> value
>>>> >>> of fgets to isalpha, which expects an int, but the return value is a
>>>> >>> char *.
>>>> >>>
>>>> >>> On Aug 4, 2014, at 18:49, Matthew Duepner
>>>> >>> <matthewduepner14 at gmail.com>
>>>> >>> wrote:
>>>> >>>
>>>> >>> I can try :) below is a copy of the email I sent to my professor
>>>> >>> asking
>>>> >>> him about the error I am receiving.
>>>> >>>
>>>> >>> Hey Professor Sable.  I am having some difficulties with my program.
>>>> >>> When I try to compile it, I get the following error: "Initialization
>>>> >>> makes
>>>> >>> integer from pointer without a cast".   I can't find anything online
>>>> >>> about
>>>> >>> how to fix this.  Additionally, I don't seem to get any errors with
>>>> >>> the
>>>> >>> "tolower" and "isalpha" but I am skeptical as to their correctness
>>>> >>> in my
>>>> >>> program.
>>>> >>>
>>>> >>>
>>>> >>> -Matthew Duepner
>>>> >>>
>>>> >>>
>>>> >>> On Mon, Aug 4, 2014 at 6:14 PM, Guan Yang <guan at yang.dk> wrote:
>>>> >>>>
>>>> >>>> You can't describe the problem over email?
>>>> >>>>
>>>> >>>> On Aug 4, 2014, at 18:07, Matthew Duepner
>>>> >>>> <matthewduepner14 at gmail.com>
>>>> >>>> wrote:
>>>> >>>>
>>>> >>>> Does anyone who is good at programming in c intend to be at the
>>>> >>>> space
>>>> >>>> tonight.  A friend and I are having trouble and could really use
>>>> >>>> some help
>>>> >>>>
>>>> >>>> -Matthew Duepner
>>>> >>>>
>>>> >>>> _______________________________________________
>>>> >>>> Blabber mailing list Blabber at list.hackmanhattan.com
>>>> >>>> https://list.hackmanhattan.com/listinfo/blabber
>>>> >>>>
>>>> >>>>
>>>> >>>>
>>>> >>>> _______________________________________________
>>>> >>>> Blabber mailing list Blabber at list.hackmanhattan.com
>>>> >>>> https://list.hackmanhattan.com/listinfo/blabber
>>>> >>>
>>>> >>>
>>>> >>> <palindrome.c>_______________________________________________
>>>> >>> Blabber mailing list Blabber at list.hackmanhattan.com
>>>> >>> https://list.hackmanhattan.com/listinfo/blabber
>>>> >>>
>>>> >>>
>>>> >>>
>>>> >>> _______________________________________________
>>>> >>> Blabber mailing list Blabber at list.hackmanhattan.com
>>>> >>> https://list.hackmanhattan.com/listinfo/blabber
>>>> >
>>>> >
>>>> > _______________________________________________
>>>> > Blabber mailing list Blabber at list.hackmanhattan.com
>>>> > https://list.hackmanhattan.com/listinfo/blabber
>>>> _______________________________________________
>>>> Blabber mailing list Blabber at list.hackmanhattan.com
>>>> https://list.hackmanhattan.com/listinfo/blabber
>>>
>>>
>>> _______________________________________________
>>> Blabber mailing list Blabber at list.hackmanhattan.com
>>> https://list.hackmanhattan.com/listinfo/blabber
>>
>>
>>
>> _______________________________________________
>> Blabber mailing list Blabber at list.hackmanhattan.com
>> https://list.hackmanhattan.com/listinfo/blabber
>
>
>
> _______________________________________________
> Blabber mailing list Blabber at list.hackmanhattan.com
> https://list.hackmanhattan.com/listinfo/blabber


More information about the Blabber mailing list