Hi All,
I'm trying to write a very simple program that captures data from stdin and displays it on stdout. Why Am I getting dirty data when the strings are printed ?
(P.S. I know that using the gets function is highly unrecommended, this program is just for testing purposes)
Code:
// MultiLine.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 8096
#define LINE_SIZE 3
void readLines(char *output);
int _tmain(int argc, _TCHAR* argv[])
{
char output[LINE_SIZE];
readLines(output);
getchar();
return 0;
}
/**
** Lê a entrada através de stdin e de acordo com o tamanho de
** cada linha separa as entradas numerando-as de acordo.
*/
void readLines(char *output) {
char input[MAX_SIZE];
int length = 0;
gets(input);
length = strlen(input);
for(int i = 0; i < length; i+= LINE_SIZE) {
strncpy(output, input + i, LINE_SIZE);
puts(output);
}
}
** dirty ** output
Code:
marcos
mar╠╠╠╠╠8■=
cos╠╠╠╠╠8■=
Thank you very much !!