Few things about arrays and fopen

Non-platform specific questions.

Few things about arrays and fopen

Postby schnellboot » Tue Jan 04, 2011 9:20 pm

First
how can I make an array[144] into this 12x12 string?
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012

And how can I save this string to a read-able txt just like that?
I only have this
Code: Select all
FILE *file;
file = fopen ("test.txt", "w");
fprintf (file, "something");
fclose (file);

"something" has to be changed if the numbers above ..

Thanks in advance ..
schnellboot
 
Posts: 819
Joined: Sat Mar 31, 2007 1:35 pm
Location: Germany
Score: 102 Give a positive score

Re: Few things about arrays and fopen

Postby hainguyentai » Wed Jan 05, 2011 9:25 am

Here is what you need guy. Just for learn only!

Code: Select all
#include "stdafx.h"
#include <stdio.h>


int _tmain(int argc, _TCHAR* argv[])
{

   char cArray[144];
   for (int j= 0; j < 12; j++)
   {
      for (int i= 0; i < 12; i++)
      {
         if (i + 1 >= 10)
         {
            cArray[i + j*12]= i + 1 - 10 + '0';
         }
         else
         {
            cArray[i + j*12]= i + 1 + '0';
         }

      }
   }
   FILE * pFile;
   pFile = fopen ("myfile.txt","w");
   char c;

   if (pFile!=NULL)
   {
      for (int j= 0; j < 12; j++)
      {
         for (int i= 0; i < 12; i++)
         {
            
            c = cArray[i + j*12];
            fputc ( (int) c , pFile );
         }
         fputs  ( "\n" , pFile );
      }
      
      fclose (pFile);
   }

   return 0;
}

hainguyentai
 
Posts: 2
Joined: Wed Jan 05, 2011 3:12 am
Score: 0 Give a positive score

Re: Few things about arrays and fopen

Postby hainguyentai » Wed Jan 05, 2011 9:26 am

Result on *.txt
Code: Select all
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
123456789012
hainguyentai
 
Posts: 2
Joined: Wed Jan 05, 2011 3:12 am
Score: 0 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest