Old 10-21-2013, 10:46 AM   #1
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,979
Default [CS]C Programming (File I/O)

Hey guys, my C programming class has been cancelled for one reason or another for the past three weeks so I'm trying to learn some of this crap on my own before my assignment is due. I'm really struggling with reading a txt file into a 2D array and can't find any snippets of code online that are really helping me.

here is my text file:
Code:
T A E D Q Q
Z H P N I U
C K E W D I
V U X O F C
B P I R G K
N R T B R B
EXIT
THE
QUICK
BROWN
FOX
This is obviously a word search puzzle.
I would like to read this into two separate 2D arrays (one for the puzzle and it will always be an nxn puzzle and n is not defined as any puzzle and word list can be used (as long as the puzzle is nxn).

here's what I have so far in an attempt to read this into an array:
Code:
#include <stdio.h>
#include <string.h>

int main()
{

    FILE *wsPtr;
    int puzzle[50][50];
    int i;
    int j;
    wsPtr = fopen("test.txt", "r");
    for(i = 0; puzzle[i][j] != EOF; i++)
    {
        for(j = 0; puzzle[i][j] != '\0'; j++)
        {
            puzzle[i][j] = fgetc(wsPtr);
            printf("%c", puzzle[i][j]);
            j++;
        }
        i++;
    }
    fclose(wsPtr);

    return 0;
}
and it outputs:

Code:
T A E D Q Q
Z H P N I U
C K E W D I
Segmentation fault (core dumped)
Any help would be greatly appreciated!
__________________


rushyrulz is offline   Reply With Quote
Old 10-21-2013, 02:07 PM   #2
reuben_tate
Kawaii Desu Ne?
Retired StaffFFR Veteran
 
reuben_tate's Avatar
 
Join Date: Dec 2007
Location: The Kawaiian Island~
Age: 30
Posts: 4,182
Default Re: [CS]C Programming (File I/O)

I'm not familiar with c but one possible issue I see is that you refer to the variable 'j' before initializing it to some value.

EDIT: Oh, I think I found your problem. Say you reach the end of the file and you store the EOF value into puzzle[i][j]. However, you increment j before checking the "puzzle[i][j]!=EOF" condition again.

EDIT: try "puzzle[i-1][j-1] != EOF" and see if that solves your problem
__________________
AMA: http://ask.fm/benguino

Not happening now! Don't click to join!



Quote:
Originally Posted by Spenner View Post
(^)> peck peck says the heels
Quote:
Originally Posted by Xx{Midnight}xX
And god made ben, and realized he was doomed to miss. And said it was good.
Quote:
Originally Posted by Zakvvv666
awww :< crushing my dreams; was looking foward to you attempting to shoot yourself point blank and missing

Last edited by reuben_tate; 10-21-2013 at 02:19 PM..
reuben_tate is offline   Reply With Quote
Old 10-21-2013, 04:46 PM   #3
Zageron
Zageron E. Tazaterra
RRR Developer & DevOps Support
AdministratorDeveloperFFR Veteran
 
Zageron's Avatar
 
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,586
Default Re: [CS]C Programming (File I/O)

These can be very complex problems.

I would highly suggest using fgetln or fgetline instead of fgetc.

In addition, you should only be creating an array of exactly the size you need. (6 by 6)

Before you continue too much, I would also suggest thinking about how arrays work in C. You'll be surprised of a few things, for example:



As you can see, the array is accessed via Array[y][x], not Array[x][y].

http://www.cplusplus.com/doc/tutorial/arrays/

----

It is much easier to bring all of the data in to memory before going through it.
The first Array should be a 6 by 6 for the puzzle.
The second array should be a 5 by 5 (or 6 if you put nulls on the ends)

The previous article should help you out. Add me on Skype if you need further assistance, as it will be easier to deal with.

Edit: Also, you have two major errors in your code.
Code:
    for(i = 0; puzzle[i][j] != EOF; i++)
    {
        for(j = 0; puzzle[i][j] != '\0'; j++)
        {
            puzzle[i][j] = fgetc(wsPtr);
            printf("%c", puzzle[i][j]);
            j++;
        }
        i++;
    }

Last edited by Zageron; 10-21-2013 at 04:54 PM..
Zageron is offline   Reply With Quote
Old 10-21-2013, 05:27 PM   #4
rushyrulz
Digital Dancing!
Retired StaffFFR Simfile AuthorFFR Music ProducerD7 Elite KeysmasherFFR Veteran
 
rushyrulz's Avatar
 
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,979
Default Re: [CS]C Programming (File I/O)

Quote:
Originally Posted by Zageron View Post
you should only be creating an array of exactly the size you need. (6 by 6)
The puzzle size is not constant. (Multiple puzzles with different sizes n will be tested).
I'm also very familiar with the indexing of two dimensional arrays, it's just for some reason I can't write into it in C.

I'll just take my loss on this assignment seeing as I'm not even close.
It's quite a mess of an assignment really seeing as there hasn't been class in 3 weeks and we haven't even talked about file I/O yet.

Thanks for the help.
__________________


rushyrulz is offline   Reply With Quote
Old 10-21-2013, 05:48 PM   #5
trumaestro
I don't get no respect
FFR Simfile AuthorFFR Veteran
 
trumaestro's Avatar
 
Join Date: Jun 2006
Age: 32
Posts: 1,332
Default Re: [CS]C Programming (File I/O)

Quote:
Originally Posted by rushyrulz View Post
The puzzle size is not constant.
For more robust code, you would need to look into dynamically allocating memory for your array using malloc() or calloc()
trumaestro is offline   Reply With Quote
Old 10-21-2013, 06:23 PM   #6
Shouka
FFR Veteran
FFR Veteran
 
Shouka's Avatar
 
Join Date: Nov 2006
Posts: 382
Default Re: [CS]C Programming (File I/O)

A majority of the entries in puzzle[][] are initially '\0'. Therefore, the index variable i is incremented many times before you actually start reading characters because the inner loop skips.
__________________
Shouka is offline   Reply With Quote
Old 10-22-2013, 02:31 AM   #7
reuben_tate
Kawaii Desu Ne?
Retired StaffFFR Veteran
 
reuben_tate's Avatar
 
Join Date: Dec 2007
Location: The Kawaiian Island~
Age: 30
Posts: 4,182
Default Re: [CS]C Programming (File I/O)

You could easily figure out what n was by just going through the first line. Then after you figure out what n is, you can easily go through the whole letter-matrix in the file since you know what your boundaries are.
__________________
AMA: http://ask.fm/benguino

Not happening now! Don't click to join!



Quote:
Originally Posted by Spenner View Post
(^)> peck peck says the heels
Quote:
Originally Posted by Xx{Midnight}xX
And god made ben, and realized he was doomed to miss. And said it was good.
Quote:
Originally Posted by Zakvvv666
awww :< crushing my dreams; was looking foward to you attempting to shoot yourself point blank and missing
reuben_tate is offline   Reply With Quote
Reply


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



All times are GMT -5. The time now is 04:53 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright FlashFlashRevolution