10-21-2013, 10:46 AM | #1 |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,981
|
[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 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; } Code:
T A E D Q Q Z H P N I U C K E W D I Segmentation fault (core dumped)
__________________
|
10-21-2013, 02:07 PM | #2 |
Kawaii Desu Ne?
Join Date: Dec 2007
Location: The Kawaiian Island~
Age: 30
Posts: 4,182
|
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 Last edited by reuben_tate; 10-21-2013 at 02:19 PM.. |
10-21-2013, 04:46 PM | #3 |
Zageron E. Tazaterra
RRR Developer & DevOps Support
Join Date: Apr 2007
Location: BC
Age: 32
Posts: 6,587
|
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.. |
10-21-2013, 05:27 PM | #4 | |
Digital Dancing!
Join Date: Feb 2006
Location: 80 billion club, NE
Age: 31
Posts: 12,981
|
Re: [CS]C Programming (File I/O)
Quote:
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.
__________________
|
|
10-21-2013, 05:48 PM | #5 |
I don't get no respect
Join Date: Jun 2006
Age: 32
Posts: 1,332
|
Re: [CS]C Programming (File I/O)
|
10-21-2013, 06:23 PM | #6 |
FFR Veteran
Join Date: Nov 2006
Posts: 382
|
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.
__________________
|
10-22-2013, 02:31 AM | #7 |
Kawaii Desu Ne?
Join Date: Dec 2007
Location: The Kawaiian Island~
Age: 30
Posts: 4,182
|
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.
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
|
|