Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

output in reverse order

0 views
Skip to first unread message

Wan Andii

unread,
Nov 17, 2009, 1:48:04 AM11/17/09
to
Hello Group,

I have a program that reads integers from an external data file. I'm
trying to figure out how to get the result in
reverse order. For eg., if file contains 1,2,3,4 then I'd like to get
the output 4,3,2,1. Thanks in advance.
Wan

int main()

{

FILE *f;
int c[20];

f=fopen("C:\File.text","r");

if (!f)
return 1;

while (f!=NULL)
{
printf("%s",c);
}


fclose(f);
return 0;

}

Alex Blekhman

unread,
Nov 17, 2009, 2:52:11 AM11/17/09
to

"Wan Andii" wrote:
> I have a program that reads integers from an external data file.
> I'm trying to figure out how to get the result in reverse order.
> For eg., if file contains 1,2,3,4 then I'd like to get the
> output 4,3,2,1.
>
> int main()
>
> {
>
> FILE *f;
> int c[20];
>
> f=fopen("C:\File.text","r");
>
> if (!f)
> return 1;
>

size_t count = <length of file> / sizeof(int);

while(count--)
printf("%d",c[count]);


Is that what you need?

Alex

Igor Tandetnik

unread,
Nov 17, 2009, 7:53:13 AM11/17/09
to
Wan Andii wrote:
> I have a program that reads integers from an external data file.

Are you sure you have one? The code you show below is not it.

> I'm trying to figure out how to get the result in
> reverse order. For eg., if file contains 1,2,3,4 then I'd like to get
> the output 4,3,2,1.

Sounds like homework to me. On the off chance it's not, what practical problem are you trying to solve?

> int main()
> {
>
> FILE *f;
> int c[20];
>
> f=fopen("C:\File.text","r");

Make it "C:\\File.text". Backslashes need to be escaped.

> if (!f)
> return 1;
>
> while (f!=NULL)
> {

Since the body of the loop doesn't change the value of the variable 'f', the condition will never become false. You have an infinite loop.

> printf("%s",c);

%s specifier expects a char* parameter, not an int*. Besides, 'c' was never initialized and contains random garbage.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not necessarily a good idea. It is hard to be sure where they are going to land, and it could be dangerous sitting under them as they fly overhead. -- RFC 1925

Barry Schwarz

unread,
Nov 18, 2009, 5:09:21 PM11/18/09
to
On Tue, 17 Nov 2009 09:52:11 +0200, "Alex Blekhman"
<tkfx....@yahoo.com> wrote:

>
>"Wan Andii" wrote:
>> I have a program that reads integers from an external data file.
>> I'm trying to figure out how to get the result in reverse order.
>> For eg., if file contains 1,2,3,4 then I'd like to get the
>> output 4,3,2,1.
>>
>> int main()
>>
>> {
>>
>> FILE *f;
>> int c[20];
>>
>> f=fopen("C:\File.text","r");
>>
>> if (!f)
>> return 1;
>>
>
>size_t count = <length of file> / sizeof(int);

Does sizeof(int) have anything to do with their text representation in
the file? For the given sample on a 32 bit system, this evaluates to
1 which seems to be short by 3.

>
>while(count--)
> printf("%d",c[count]);
>
>
>Is that what you need?
>
>Alex

--
Remove del for email

Alex Blekhman

unread,
Nov 19, 2009, 7:55:59 AM11/19/09
to
"Barry Schwarz" wrote:
> Does sizeof(int) have anything to do with their text
> representation in the file? For the given sample on a 32 bit
> system, this evaluates to 1 which seems to be short by 3.

Good catch. I was thinking about binary files with integers for
some reason.

Alex

0 new messages