LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-22-2008, 02:00 PM   #1
kyle4579
LQ Newbie
 
Registered: May 2008
Posts: 13

Rep: Reputation: 0
Can somebody tell me why this program wont do what i want!?


I am only new to programming and to practice, I have been writing some of my own programs. This one is fairly useless but I think it should work none the less. It is to do with character input and output.

This program should read a text file and print it backwards! :

Quote:
#include <stdio.h>

int count();
main()
{
int c, t, i;
int write[count()];
t = count();
c = getchar();

while (c!=EOF)
{
write[t] = c;
--t;
c = getchar();
}

for (i = 0; i <= count(); ++i)
{putchar(write[i]);}

}

int count()
{
int c, n;
c = getchar();
n = 0;

while (c!=EOF)
{
++n;
c = getchar();
}

return n;
}
When I try to run it in the terminal, I type the commands:

Quote:
[john@localhost ~]$ gcc back.c
[john@localhost ~]$ ./a.out <test.txt
but nothing happens!

Thanks for any guidance.
 
Old 07-22-2008, 02:30 PM   #2
indienick
Senior Member
 
Registered: Dec 2005
Location: London, ON, Canada
Distribution: Arch, Ubuntu, Slackware, OpenBSD, FreeBSD
Posts: 1,853

Rep: Reputation: 65
Try what I've posted below - I'm at work right now, and don't have access to a Linux box (or C compiler for that matter). I've just cleaned up things a bit - but I am by no means seriously proficient in C.

To be perfectly honest, I don't know the exact reason why your code isn't working. It might be some silly lexical error where you have something initialized too early, or some assignments somewhere aren't being met.

This may seem like a silly notion, but make sure there's something inside of test.txt.

Code:
#include <stdio.h>

int count();

int main()
{
	int c, i;
	int limit = count();
	int write[limit];	
	
	i = (limit + 1);
	
	while ((c = getchar()) != EOF)
		write[--i] = c;
		
	for (i = 0; i < ++limit; i++)
		putchar(write[i]);
}

int count()
{
	int n = 0
	
	while (getchar() != EOF)
		n++;
		
	return n;
}

Last edited by indienick; 07-22-2008 at 02:34 PM.
 
Old 07-22-2008, 03:54 PM   #3
wget
LQ Newbie
 
Registered: Apr 2008
Distribution: Slackware 12.0 Kernel 2.6.25, LFS 2.6.25.1 Kernel
Posts: 25

Rep: Reputation: 15
Please ignore

Last edited by wget; 07-22-2008 at 03:57 PM. Reason: I was talking nonsense : )
 
Old 07-22-2008, 04:13 PM   #4
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
First error:
Quote:
Originally Posted by kyle4579 View Post
int write[count()];
This shouldn't work at all (unless I don't know something about C, and I don't think so). In C++ this doesn't work and I'm surprised if this one doesn't give even warnings in C.
When you declare array, you should give it constant size that won't ever change. Constant size must be declared at compile time, this can't be variable. If you want to set array size in runtime, then allocate it dynamically (malloc, realloc, etc.). You can't just pass return value of function.

Second error:
Quote:
Originally Posted by kyle4579 View Post
int count()
{
int c, n;
c = getchar();
n = 0;

while (c!=EOF)
{
++n;
c = getchar();
}
This won't work.
After call to a count() stream reaches eof. When you try to read from input stream after that, you won't get all characters once again from beginning. You'll get only eof error.

Quote:
Originally Posted by kyle4579 View Post
This one is fairly useless but I think it should work none the less.
it contains errors and shouldn't work.

This one works:
Code:
#include <stdlib.h>
#include <stdio.h>

int arraySize = 0;
int arrayCapacity;
char *arrayData = 0;

void initArray(){
    arrayCapacity = 32;
    arrayData = malloc(arrayCapacity);
}

void growArray(){
    arrayCapacity *= 2;
    arrayData = realloc(arrayData, arrayCapacity);
}

void appendChar(char c){
    if (arraySize >= arrayCapacity)
	growArray();
    arrayData[arraySize++] = c;
}

int main(int argc, char** argv){
    char c;
    int i;
    initArray();
    for (c = getchar(); c != EOF; c = getchar())
	appendChar(c);

    for (i = arraySize - 1; i >= 0; i--)
	putchar(arrayData[i]);

    free(arrayData);
    return 0;
}
 
Old 07-22-2008, 04:51 PM   #5
kyle4579
LQ Newbie
 
Registered: May 2008
Posts: 13

Original Poster
Rep: Reputation: 0
Hi guys. Thanks for the help indienick. And ErV, I compiled and ran yours and it does exactly what I'd been trying, thanks v much! The only thing is, I am very new to this, and I understand the errors I made in my code now, but I don't fully understand the coding which precedes the main function...! Would you have time to go over it briefly, please? Thanks for the help so far though, much appreciated.
 
Old 07-22-2008, 05:38 PM   #6
ErV
Senior Member
 
Registered: Mar 2007
Location: Russia
Distribution: Slackware 12.2
Posts: 1,202
Blog Entries: 3

Rep: Reputation: 62
Quote:
Originally Posted by kyle4579 View Post
but I don't fully understand the coding which precedes the main function...! Would you have time to go over it briefly, please? Thanks for the help so far though, much appreciated.
first, check manpages for malloc, free and realloc. (man malloc, etc.).
And I think reading books on C++ and practice (and a lot of it) will help more than my explanation. If you love programming (this is required) then you'll get all concepts eventually during practice.

Also, you'll get much better answers if you'll point out which parts you don't understand. I can't cover pointers, basic constructs like resizeable arrays, memory allocation functions, etc. in one post. (it's all explained in books).

Brief description:
generally variable/function names speak for themselves.

"char* arrayData = 0"
A pointer ("address") to the the memory block where we hold chars we've read. You may think about it as array with dynamic size, or the address of the array. Block is initialized with malloc (see "man malloc"), and freed with "free" (all allocated memory must be freed to prevent memory leaks). Realloc() changes size of block we've allocated, but preserves all existing data.

"int arraySize = 0;"
tells how many elements in array right now.

"int arrayCapacity;"
tells how many elements dynamic array can hold.
If we allocate block for 32 chars and then try to write 33th char, then we'll get segfault (if we are lucky. If we aren't (on old compilers, etc, real-mode systems, etc.), then it'll overwrite some memory region nearby, and we'll spend eternity trying to figure out "why things doesn't work as expected").

"void initArray(){"
function that initializes array. Sets that array can hold 32 elements and allocates block of that size.

"void growArray(){"
called when arraySize is equal to capacity - i.e. no more chars can be placed in our memory block. Sets it so array can hold 2x more elements then before and reallocates memory block according to new capacity.

"void appendChar(char c){"
appends char to array, changes number of elements accordingly, checks if array need to grow.

Can't explain much more, you'll get better answers if you ask what exactly you don't understand.
 
Old 07-24-2008, 07:28 AM   #7
Micky_123
LQ Newbie
 
Registered: Dec 2007
Posts: 17

Rep: Reputation: 0
Hi,

I believe your initla code should have posted some warnings. For a better programming, treat warnings as errors only.

cheers
Micky
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple program wont compile BACTRATE Linux - Software 14 07-24-2008 12:57 PM
Program wont start on boot SBN Linux - General 2 11-10-2007 03:41 PM
wine program wont work if cups running nephish Linux - Software 0 09-23-2006 01:54 PM
c++ program wont work, HELP! computer_tom Programming 6 08-20-2006 12:36 PM
C++ - Wondering why my program wont loop? eclipse75 Programming 2 03-27-2005 09:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:20 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration