Skip to content

Instantly share code, notes, and snippets.

@quolpr
Last active August 29, 2015 14:14
Show Gist options
  • Save quolpr/58d23ecf9bea047b462d to your computer and use it in GitHub Desktop.
Save quolpr/58d23ecf9bea047b462d to your computer and use it in GitHub Desktop.
laba
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
bool file_valid(char *filename) {
_strlwr_s(filename, 100);
char* const unsupported[] = {
"con", "prn", "aux",
"clock$", "nul", "com1",
"com2", "com3", "com4",
"com5", "com6", "com7",
"com8", "com9", "lpt1",
"lpt2", "lpt3", "lpt4",
"lpt5", "lpt6", "lpt7",
"lpt8", "lpt9"};
for (int i = 0; i < 22; i++) {
if (strcmp(filename, unsupported[i]) == 0) {
return false;
}
}
return true;
}
int _tmain(int argc, _TCHAR* argv[])
{
setlocale(LC_ALL, "rus");
FILE * infile;
char *filename = new char[100];
std::cout << "Введите название файла: ";
std::cin >> filename;
if (std::cin.fail()) {
std::cout << "Не верный формат" << std::endl;
exit(0);
}
fopen_s(&infile, filename, "r");
if (!file_valid(filename) || infile == NULL) {
std::cout << "Файл не существует или не верное имя" << std::endl;
exit(0);
}
char begin_ch;
std::cout << "Введите символ: ";
std::cin >> begin_ch;
char ch;
int char_pos = 0;
char *word = new char[10000];
bool word_found = false;
while ((ch = fgetc(infile)) != EOF){
if (ch == ' ' && word_found) {
break;
}
if (ch == ' ' && !word_found) {
char_pos = -1;
}
if (char_pos == 0 && tolower(ch) == tolower(begin_ch)) {
word_found = true;
}
if (word_found) {
word[char_pos] = ch;
}
char_pos++;
}
if (word_found) {
word[char_pos] = '\0';
std::cout << "Ваше слово: " << word << std::endl;
}
else {
std::cout << "Слово не найдено(" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment