1

I want read text file and store in array then show.

This is my code:

  int i = 0, line = 5;
  char ch[100];

  FILE *myfile;
    myfile = fopen("test.txt","r");
    if (myfile== NULL)
    {
    printf("can not open file \n");
    return 1;
    }

    while(line--){
    fscanf(myfile,"%s",&ch[i]);
    i++;
    printf("\n%s", &ch[i]);
    }

    fclose(myfile);

    return 0;
}

This is my text:

test 123562

856

59986

But result:

est

2356

56

9986

What is wrong? :(

2
  • Learn how to use a debugger, and step through the code in the debugger line by line, while keeping track of i, ch and &ch[i]. Commented Jan 9, 2015 at 9:02
  • 1. %s in fscanf breaks at space char; 2) you do printf after i++, i.e. show one char after scanned. It is better to use fgets insteaf of fscanf in your case. Commented Jan 9, 2015 at 9:04

4 Answers 4

5

ch[i] is holding a single character. Statement fscanf(myfile,"%s",&ch[i]); will scan string to ch[i] which can hold only one character. There is no place for '\0' which leads your program to undefined behavior. Change

fscanf(myfile,"%s",&ch[i]);  

to

fscanf(myfile,"%s",ch);


Previous answer was wrong. Behavior of program is well defined but you are scanning the file in a wrong manner. Your program will work as expected if you place i++; after printf statement.

while(line--){
    fscanf(myfile,"%s",&ch[i]);
    printf("\n%s", &ch[i]);
    i++;
}   

The reason is that &ch[i] is a pointer to the ith element of the array and string will be stored in array starting at position i. For the input given, this will work because the given array is large enough to hold the string.

You can do this as:

while(line--){
    fscanf(myfile,"%s",ch);
    printf("\n%s", ch);
    i++;
}   

but it will overwrite the array ch each time a string is scanned to it. Better to use a two dimensional array to store strings and read file with fgets.

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

6 Comments

It would nice to explain that (s)he wants to scan for a string, not a single character.
The variable i is not needed in the first place.
tanks but i need access to my array. i need ch[0],ch[1],ch[2],.. for analyze
While ch[i] is only a single character, remember that &ch[i] is equivalent to ch + i, and when i is e.g. 0 then that's just plain ch. So &ch[i] is a pointer to a string that starts at index i in the array ch.
@JoachimPileborg; Yes. Point to be noted!
|
2

You're not going to be able to fit five lines in the single char ch[100] array; that's just an array of 100 characters.

You can make it an array of arrays, i.e. char ln[5][100] which will give you room for five lines of 100 characters each.

Then you of course need to index into that array in the loop, i.e.:

for(int i = 0; i < 5; ++i)
{
  if(fgets(ln[i], sizeof ln[i], myfile) == NULL)
  {
    fprintf(stderr, "Read error on line %d\n", i);
    exit(1);
  }
}

This uses fgets() which is much better suited at reading in whole lines; fscanf() will stop at whitespace with %s which is seldom what you want.

Comments

1

There is no need to use the ampersand in the scanf while getting the string. Make that into like this.

fscanf(myfile,"%s",&ch[i]);

to

fscanf(myfile,"%s",ch);

&ch[i] It will get the character for i th position in that array. If you want to get like that you can use the %c instead of %s. And change this one to.

printf("\n%s", ch);

While printing the string when you use the ampersand(&) that will access the address of that variable.

1 Comment

Changing fscanf(myfile,"%s",&ch[i]); to fscanf(myfile,"%s",ch); will not solve the problem.
-2
  1. The program developed must be able to read the input files containing matrix A and matrix B using fopen function a. Matrix A and B of different size may be stored in different input file (if required).
  2. Scan and assign matrix A and B as array using fscanf function and for loop
  3. Perform matrix operations a. Add matrix A and B b. Subtract matrix A and B c. Multiply matrix A and B
  4. Use conditional statement if or switch for switching between 3, 4 and 5 elements matrix.
  5. Print all input matrices and results obtained in a new file called output.dat using fprintf function.
  6. The output.dat file must have a header with the following information: a. Student name b. Student matric number c. Class section d. Lecturer name e. Project title
  7. Below the header, the output file must contain matrix A and B and the results from matrix operation. Use matrix A and B as given below:

1 Comment

wheres 1 and 2?

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.