So I have a .txt file that contains an ASCII image. I have to scan that file into a C program and print that ASCII image inverted which basically means printing the last character first and then the second last character in second position and so on.
The file contains 30 lines and each line contains 100 characters. That is why I have given the string array value 3100. I thought if I used an inverted loop and printed the characters inverted, that would do the trick but it doesn't do it.
So I don't know what's the problem here. Also, I'm very naive at C and this is my first question here. So please forgive me if I have done any mistakes in asking the question. And it would be very nice if you provide me with a code to solve this problem and explain it to me. I'm very new to C so I might not understand complex codes. And also suggest me what are the concepts that I need to learn to handle this type of problems. Thank you.
This is my code -
#include<stdio.h>
int main (){
char str[3100];
int i;
FILE *output=fopen("output.txt", "w");
FILE *input=fopen("input.txt", "r");
for(i=0;i<3000;i++)
{
fscanf(input, "%s", &str[i]);
}
for(i=3000;i>0;i--)
{
fprintf(output, "%c", str[i]);
}
return 0;
}
You are scanning strings instead of characters. And is the ASCII image exactly 3000 characters long including the the line breaks? You may better check for EOF in a while loop. You're also missing the last character in your second for loop:
[...]
fscanf(input, "%c", &str[i]);
[...]
for(i=2999;i>=0;i--)
{
fprintf(output, "%c", str[i]);
}
return 0;