我正在尝试将文件的内容复制到LIFO.my文件中的链接列表中,但是当我将其存储在链接列表中时,它会被存储为 - QWERTY
YTREWQ
之前的空格。附加空格来自哪里?这是我的代码 -
http://ideone.com/siR5dI
答案 0 :(得分:0)
空白是' \ n'文件末尾的新行字符或行终止符,将添加到LIFO队列。
$ ./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:%c\n",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值。
答案 1 :(得分:0)
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')