0

I have an input text file with some instructions and, starting with line 7, several lines of text. Something like this:

hi gBroThuo oWdbmna eo mt ce oneain,nDustuh o n
Ade ds,bpopoonf  oneigno abro wmt  eIw
n,Yrtyt hlil t .s Ble a  meyboefr rtIhoyod
wla rimw yidehl. kes ,oyn L  af
fu;AcMadmdnae  nddmh ita behsctr rft iHdo"l,sie g"hu!,n eoaecMBt-
- h

I need to store the text to be stored in a char array (including the new line characters). What functions can I used to read and store this text to a single char array?

2 Answers 2

1
char fileBuf[MAXFILE];
FILE *f;
int c;
size_t i;

if (f = fopen("filename", "r")) {
    for (i = 0; i < (MAXFILE - 1) && (c = getc(f)) != EOF; ++i)
        fileBuf[i] = c;
    fclose(f);
} else perror("Could not open file");

EDIT: you said you wanted to skip the first 7 lines.

int x;
char line[MAXLINE];
for (x = 0; x < 7; ++x)
    fgets(line, MAXLINE, f); /* skips first 7 lines, so now file pointer
                                will point to data after the 7 lines */
Sign up to request clarification or add additional context in comments.

Comments

0

You could do something like this:

int i = 0;
while(fscanf(inputFile, %c, &charArray[i])!=EOF)
   i++;

Comments

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.