Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 23rd, 2005, 5:23 PM   #1
ridley
Newbie
 
Join Date: Feb 2005
Location: Orlando, FL
Posts: 8
Rep Power: 0 ridley is on a distinguished road
gets() produces garbage [would appreciate some more help]

This compiles, but it only produces garbage
any ideas?

#include <stdio.h>

#define MAX 20
int counter;
int stack[MAX];
char maze[20][20];      //value at position[h][v]

void push (int x);
int pop(void);
void getvals();

FILE *ifp, *ofp;

int main(void)
        {
        char filename[13]; //temp filename string


        //getting filenames and assigning pointers
        printf("\nEnter the name of the maze file.\n");
        scanf("%s", filename);
        ifp = fopen(filename, "r");
        printf("\nEnter the name of the output file.\n");
        scanf("%s", filename);
        ifp = fopen(filename, "w");
        printf("\nOrder of visiting: Up Down Left Right");

        getvals(maze);
        fclose(ifp);
        fclose(ofp);

        }
void push (int x)
        {
        if (counter>MAX)
                {
                printf("Stack full.\n");
                return;
                }

        stack[counter] = x;
        counter++;
        }

int pop(void)
        {
        counter--;
        if (counter<0)
                {
                printf("Stack underflow.\n");
                return 0;
                }
        return stack[counter];
        }

void getvals()
        {
        int i, j;
        char tempstring[20];

        for (i = 0; i <= 20; i++)
                {
                fgets(tempstring, 20, ifp);
                printf("\n");
                for (j = 0; j <= 20; j++)
                        printf("%c", tempstring[j]);
                        // *maze[i][j] = tempstring[j];
                } // end for (i)
        } //end getvals()

I'm trying to read this robotfile.txt into a 2-dimensional array
Quote:
11111111111111111111
11111S01111111101111
11111101111111101111
11111100000000001111
11111110111111101111
11111110111111101111
11111110111111101111
11111111111111101111
11111111110000001111
11111111110111101111
11111111110111101111
11111111110111101111
11111111111111101111
11111111111111101111
11111G11111111101111
11111011111111101111
11111000000000001111
11111111111111111111
11111111111111111111
11111111111111111111
Any help is appreciated

Last edited by ridley; Feb 24th, 2005 at 12:54 AM. Reason: new problem(s)
ridley is offline   Reply With Quote
Old Feb 23rd, 2005, 6:01 PM   #2
tempest
Programming Guru
 
tempest's Avatar
 
Join Date: Oct 2004
Posts: 1,041
Rep Power: 5 tempest is on a distinguished road
Send a message via ICQ to tempest Send a message via AIM to tempest Send a message via Yahoo to tempest
Two quick things i noticed...

1) you are using fgets on a file pointer opened with the "w" flag, this should be "r".
2) getvals doesn't take an argument...
__________________

tempest is offline   Reply With Quote
Old Feb 23rd, 2005, 11:17 PM   #3
ridley
Newbie
 
Join Date: Feb 2005
Location: Orlando, FL
Posts: 8
Rep Power: 0 ridley is on a distinguished road
That did it, thanks!
ridley is offline   Reply With Quote
Old Feb 24th, 2005, 12:53 AM   #4
ridley
Newbie
 
Join Date: Feb 2005
Location: Orlando, FL
Posts: 8
Rep Power: 0 ridley is on a distinguished road
I fixed the file pointers and the function calls and prototypes but I am still having problems.
I think the problem is with reading the file into a two-dimensional array, or printing the array out, I'm not sure which.
I don't think it is the size of my arrays (I have tried both 20 and 21 to account for the eos char), but I think it may have something to do with the relationship between functions, arrays, and pointers...

see above for an example of what the program is reading in

#include <stdio.h>

char maze[21][21];      //value at position[h][v]

void getvals();

FILE *ifp, *ofp;

int main(void)
	{
	char filename[13]; //temp file name string
	
	//getting filenames and assigning pointers
	printf("\nEnter the name of the maze file.\n");
	scanf("%s", filename);
	ifp = fopen(filename, "r");
	printf("\nEnter the name of the output file.\n");
	scanf("%s", filename);
	ofp = fopen(filename, "w");
	printf("\nOrder of visiting: Up Down Left Right");
	
	getvals();
	fclose(ifp);
	fclose(ofp);
	
	}

void getvals()
	{
	int i, j;
	char tempstring[20];
	
	for (i = 0; i <= 20; i++)
		{
		fgets(tempstring, 21, ifp);
		printf("\n");
		for (j = 0; j <= 20; j++)
			{
			maze[i][j] = tempstring[j];
			} // end for (j)
		} // end for (i)
	for (i = 0; i <= 20; i++)
		for (j = 0; j <= 20; j++)
			printf("%c", maze[i][j]); //end for (j)
		// end for (i)
	} //end getvals()
As always, any help is appreciated
ridley is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC