0

I want to write a method called print array that has 2 parameters seen below.

I want to, most likely using a for loop, iterate through the array of characters and pass each character to the output text file to be printed on that file on the same line. so if the array has a, b, c, d, e, f, g. in the file I want it to display abcdefg. I dont really know how to get it started.

void printArray(char * array, FILE * fout)
{
    //i think using a for loop is the way to go, i just dont know exactly what to do after
}
1
  • 1
    please get a copy of K&R if you are serious about your basic c class Commented Mar 14, 2012 at 8:48

2 Answers 2

2

Try this:

void printArray(char * array, FILE * fout, int MAX_CHAR)
{ 
     int i;
     fout = fopen("file.txt","a+");      /* open the file in append mode */
     for (i=0; i<MAX_CHAR; i++)
          fprintf(file,"%c",*(array+i)); /* write */ 
     fclose(file);                       /* close the file pointer */ 

     return 0; 
}
Sign up to request clarification or add additional context in comments.

Comments

2

It's called fputs(). POSIX standard, because this problem has been solved before by multiple people who also needed to print character arrays (or to "put a string") into a FILE.

You can either just use the code as-is from your friendly local standard C library, or you can read it to figure out what you need to do to do so yourself, should you feel the need.

EDIT: try the following to get you started https://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=7425&lngWId=3

2 Comments

i have no idea what this stuff means though. im in a basic c class, the terms in that link dont look familiar to me.
@anthony which is why i edited it out; find a simpler example; check new link for a simpler one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.