5

I'm really new to programming, and after thinking hard about this for a week for a summer project, I'd really appreciate some help!

I'm trying to read in a long text file, which is just a long string (NB: not an actual programming string) of letters, and then put each letter into its place in the grid (the aim of the program is ultimately to solve a wordsearch) so far I've come up with the program below, which doesn't seem to be producing a grid, but rather just reprints the text file, preceded by the following:

{\rtf1\ansi\ansicpg1252\cocoartf1138\cocoasubrtf510    
{\fonttbl\f0\fmodern\fcharset0 Courier;}
{\colortbl;\red255\green255\blue255;}
\paperw11905\paperh16837\margl1440\margr1440\vieww10800\viewh8400\viewkind0
\deftab720
\pard\pardeftab720

\f0\fs24 \cf0 

The program that I've written is this:

#include <stdio.h>
#include <stdio.h> 
#include <stdlib.h> 
#include <math.h>
#include <stdbool.h>

int main()
{
    int i,j;
    char myarray[26][26],x;
    FILE *myfile;

    for (j=0; j<26; j++)                 //initialise array elements all to zero
    {
        for (i=0; i<26; i++)
        {
            myarray[i][j]=0;
        }
    }

    myfile=fopen("*redacted*","r");
    if (myfile!=NULL) //check file actually opened
    {
        for (i=0; i<26; i++)
        {
            for(j=0; j<26; j++)
            {
                fscanf(myfile,"%c",&x); //read the values in
                myarray[i][j]=x;
            }
        }
        // data is now in the array called myarray
        fclose(myfile);        
    }

    else
    {
        printf("File not found");
    }

    for(i=0;i<26;i++)
    {
        for(j=0;j<26;j++)
        {
            printf("%c",myarray[i][j]);
        }
    }

}

Thank you for any help you can offer

5
  • 2
    +1 for indenting the code (more or less) so it's readable, and for doing error checking too. Rare bird. Commented Jul 17, 2013 at 14:51
  • 1
    However, a question: what do you mean by "it doesn't seem to be producing an array"? You do have the array: it's the myarray object (variable). Commented Jul 17, 2013 at 14:52
  • the program outputs the text file like so: Mniparogocomputerhardwareywoieeoonodsmecivedegarotspesdfnidpctatrtnytrrrorcreoggimtnatosotmdroeoamupeuciwngbytekmgtoaeanrsnmgtdtittiimrehpponnnidtosdruamhvoaetmogeiuiutemouseopommaeyreaamrtvtfltnmslscmeedctdepulmeaioerkatluieoaredsitasyiamtrrrooeleeccrcyprkyunlontmewgsirsyricopcpoabgpetifgrddyorecsbimrdereapnnckoorecarfrhitnoiprscgstripeoratieotbpaornaoosetyeeatpumotrrmwnarrterrwmprraiopitaarceeaotkeduscdeipoesoskpttoudm preceeded by the strange text I copied in the post. I've tried checking that letters are where they should be, and they don't appear Commented Jul 17, 2013 at 14:57
  • 1
    @H2CO3 It seems OP wanted to say "grid" instead of "array"; i changed the question (hope it's a correct guess) Commented Jul 17, 2013 at 15:22
  • @anatolyg So that's formatting. Fair enough, thank you. Commented Jul 17, 2013 at 15:22

2 Answers 2

4

Here's the beauty of C:

You can read the file in a single operation, and save yourself the looping:

Something like

fread(myArray, sizeof(myArray), myfile)

You should probably initialize the array to all zeros before you do this, though:

char myArray[26][26] = { 0 };

Or fill it with zeroes if you don't initialize it:

 memset(myArray, 0, sizeof(myArray));

Also, you might want to print a newline ("\n") at the end of each outer loop in your printing section: otherwise the file contents will appear as one long, continuous string.

Sign up to request clarification or add additional context in comments.

14 Comments

One of the few beauties of C
This, however, encourages some bad practice, which is harmful to recommend to a beginner. Firstly, don't cast to void *, it's superfluous and the only thing it does is decreasing readability. Two, do check the return value of fread() - OP does seem to know about error handling, don't mislead him, because fread() may not read the entire array, in which case one has to retry. Three, use sizeof(array) instead of a hard-coded size, it's safer.
But the last line in this answer is critical to the OP - the output that is being generated will look exactly like the input file unless you add newlines to see that it's an array.
@H2CO3 Agreed on all points, but the aim was to make clear to him how to accomplish what he was doing in another way.
@user2591837 Let me correct myself: you should be rather declaring a 26 x 27 element array, and leaving room for one terminating NUL character at the end, so it's easier to print the lines.
|
2

I'm not going to do your summer project, but here's some remarks:

1) The text file is not a text file but an RTF file, which is text with formatting. You probably made/saved it using TextEdit. If so, use the 'Make Plain Text' menu item to fix this.

2) Your program seems to be doing the following: read 26x26 (why 26?) characters from a file (including spaces, newlines etcetera), put them in a 26x26 array, and then print them one by one. I'm not clear what you're trying to achieve, but it appears to have something to do with formatting the array, in which case you will probably want to think about how to handle whitespace characters in the input, and printing them yourself when generating output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.