Считать информацию из файла и вывести по центру - C (СИ)
Формулировка задачи:
Здравствуйте, подскажите пожалуйста в чем косяк программы? Программа считывает 2 строчки из файла и выводит 2 раза подряд первую строчку расположенную по центру и потом вторую строчку с самого начала строки.
Содержимое файла test.txt
#include <stdio.h>
#include <string.h>
#define MAX_LEN 80
char* middle(char* str);
int main(void){
char *str = "test";
read_file();
//printf("%s\n", middle(str));
return 0;
}
char* middle(char *str)
{
static char buf[MAX_LEN + 1];
int spaces_needed, str_len;
str_len = strlen(str);
if (str_len >= MAX_LEN ) return str;
spaces_needed = (MAX_LEN - str_len) / 2;
memset(buf, ' ', spaces_needed);
strcat(buf + spaces_needed, str);
return buf;
}
int read_file()
{
FILE *readed_stream;
char path_to_file[1000];
int readed_buffer = 0;
char* data[20];
printf("Enter path to readable file: ");
if(scanf("%s",&path_to_file))
{
if(readed_stream = fopen(path_to_file,"r"))
{
do{
fgets(data,sizeof(data),readed_stream);
printf("%s",middle(data));
}while(!feof(readed_stream)); //readed_stream!=EOF
fclose(readed_stream);
//printf("%s",data);
}
}
}first string. second string.
Решение задачи: «Считать информацию из файла и вывести по центру»
textual
Листинг программы
#include "stdafx.h";
#include <stdio.h>
#include <string.h>
#define MAX_LEN 80
char* middle(char *str)
{
static char buf[MAX_LEN + 1];
memset(buf,0,MAX_LEN + 1);
int spaces_needed, str_len;
str_len = strlen(str);
if (str_len >= MAX_LEN ) return str;
spaces_needed = (MAX_LEN - str_len) / 2;
memset(buf, ' ', spaces_needed);
strcat(buf + spaces_needed, str);
return buf;
}
int read_file()
{
FILE *readed_stream;
char path_to_file[1000];
int readed_buffer = 0;
char data[20];
printf("Enter path to readable file: ");
if(scanf("%s",&path_to_file))
{
if(readed_stream = fopen(path_to_file,"r"))
{
do{
fgets(data,sizeof(data),readed_stream);
printf("%s",middle(data));
}while(!feof(readed_stream)); //readed_stream!=EOF
fclose(readed_stream);
//printf("%s",data);
}
}
return 0;
}
int main(void){
char *str = "test";
read_file();
//printf("%s\n", middle(str));
return 0;
}