c - reading a text file into an array -


i'm new programming, , after thinking hard week summer project, i'd appreciate help!

i'm trying read in long text file, long string (nb: not actual programming string) of letters, , put each letter place in grid (the aim of program solve wordsearch) far i've come program below, doesn't seem producing grid, rather reprints text file, preceded 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 i've written 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;      (j=0; j<26; j++)                 //initialise array elements 0     {         (i=0; i<26; i++)         {             myarray[i][j]=0;         }     }      myfile=fopen("*redacted*","r");     if (myfile!=null) //check file opened     {         (i=0; i<26; i++)         {             for(j=0; j<26; j++)             {                 fscanf(myfile,"%c",&x); //read values in                 myarray[i][j]=x;             }         }         // data in 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 can offer

here's beauty of c:

you can read file in single operation, , save looping:

something

fread(myarray, sizeof(myarray), myfile) 

you should initialize array zeros before this, though:

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

or fill zeroes if don't initialize it:

 memset(myarray, 0, sizeof(myarray)); 

also, might want print newline ("\n") @ end of each outer loop in printing section: otherwise file contents appear 1 long, continuous string.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -