我正在尝试将文件的内容复制到链表中 LIFO.my 该文件具有此数据QWERTY
但是当我将其存储在链表中时,它会存储为- YTREWQ
,并在Y
之前带有一些空格。额外的空间从何而来?这是我的代码-http://ideone.com/siR5dI
空格是添加到后进先出队列的文件末尾的""换行符或行终止符。
$ ./LIFO.out inputfile
0:Q
1:W
2:E
3:R
4:T
5:Y
6:
$ cat inputfile
QWERTY
$
以上是我通过更改以下行获得的输出:
int count = 0; // Add count initialization
while(ch!=EOF)
{
printf("%d:%cn",count++,ch); // Print count to see how many chars are found in the file
head1=insert(ch,head1);
ch=getc(file1);
}
要打印 ascii 值,请使用以下命令更新上述代码片段:
printf("%d:%c(%d)n",count++,ch,ch);
它给了我以下输出:
0:Q(81)
1:W(87)
2:E(69)
3:R(82)
4:T(84)
5:Y(89)
6:
(10)
这 10 是新换行的 ascii 值。
the posted code does not cleanly compile.
if it did cleanly compile, then it would lose the
contents of the 'head1' list pointer.
the main function
should be confirming the value of 'argc'
and displaying a 'usage' message if that value
is not 2
the code has a missive memory leak because
the many malloc() areas are not passed to free()
for several reasons, the returned value from malloc() 9and family)
should not be cast
the returned value from malloc() needs to be checked
to assure successful operation
this line in insert(): 'temp->next=head;'
is placing the new node at the beginning of the array
this line in insert(): 'head=temp;'
is copying pointers
what is really need is something more like:
'*head = temp'
the insert() function is declared as returning a pointer
to a Node. However, it does not do so.
that is one of the two compiler warnings raised.
(enable all warnings so you will see these problems)
(warnings should be corrected, not ignored)
the main() function has two parameter
and the first one 'argc' is not being used.
this one of the two compiler warnings raised.
properly checking/responding to the contents of
'argc' would correct that problem.
BTW: after 'head1' is set to point to the first Node
you do not want to be ever changing it.
this is usually a 'special case'
handled by the insert() function
the print() function fails to handle the case
of no Node in the linked list of Nodes
the main() function needs to check that the
input char is either numeric or alpha
before calling the insert() function
as an assistance, the call to insert() should be passing
a double pointer that points to 'head1'
not the contents of 'head1'
suggest::
1) do not return a pointer from insert()
2) use insert( ch, **&head1 );
where insert prototype is: void insert( char, struct Node ** )
3) check the number of command line parameters (using argc)
and display a 'usage' messsage if there is a problem
4) the insert() function is inserting each node at the beginning
of the linked list, which is why it is printing the data backwards
5) correct the print() function to allow for an empty linked list
6) code like it was C and not Pascal, I.E.
place the sub functions after main()
and use prototype statement.
7) when a I/O statement fails, use 'perror()' function
which will place statements to stderr,
including the built in message that explains why the failure
8) there are reasons to use 'fgetc()' rather than 'getc()'
including the returned value is an 'int', so EOF
can actually be recognized
(EOF is an 'int', not a 'char')