string.h

Bu başlık kilitlidir. Yeni mesajlar gönderilemez veya mevcut mesajlar düzenlenemez.
gdumanli
18-04-2006, 22:39   |  #1  
OP Taze Üye
Teşekkür Sayısı: 0
4 mesaj
Kayıt Tarihi:Kayıt: Mar 2006

arkadaşlar elimde bir kod var bi text dosyasından metin okutup işlem yaptırıyor.ama ben dosyadan okutma hakkında hiçbir bilgiye sahip degilim.bu texti dosyadan okutmasam main içinde array e atasam.bu kod uzerinde yapılabilir mi?
lutfen bi bakarmısınız cok acil...

Bir paragrafta bulunan sat#253;rlar#253;n uzunluklar#253;n#253; e#254;itleyen (word wrap) program. Sat#253;rlar#253;n uzunluklar#253;n#253;n ayarlanmas#253; için önce fazla kelimeler alt sat#253;rlara kayd#253;r#253;l#253;yor, sonra da kelimeler aras#253;ndaki bo#254;luklar artt#253;r#253;larak sat#253;r uzunluklar#253; e#254;itleniyor. LINE_SIZE sembolik sabitini de#240;i#254;tirerek sat#253;rlar#253; farkl#253; uzunluklara ayarlayabiliriz.

input.txt
-------------------------------------------------
Oh, little prince! Bit by bit I came to understand the secrets of your sad little life . . . For a long time you had found your only entertainment in the quiet pleasure of looking at the sunset. I learned that new detail on the morning of the fourth day, when you said to me:
"I am very fond of sunsets. Come, let us go look at a sunset now."
"But we must wait," I said.
"Wait? For what?"
"For the sunset. We must wait until it is time."
At first you seemed to be very much surprised. And then you laughed to yourself. You said to me:
"I am always thinking that I am at home!"
Just so. Everybody knows that when it is noon in the United States the sun is setting over France.
If you could fly to France in one minute, you could go straight into the sunset, right from noon. Unfortunately, France is too far away for that. But on your tiny planet, my little prince, all you need do is move your chair a few steps. You can see the day end and the twilight falling whenever you like . . .
"One day," you said to me, "I saw the sunset forty-four times!"
And a little later you added:
"You know--one loves the sunset, when one is so sad . . ."
"Were you so sad, then?" I asked, "on the day of the forty-four sunsets?"
But the little prince made no reply.

from the Little Prince by Antoine de Saint Exupry

Ekran Ç#253;kt#253;s#253;:input file: input.txt
output file: output.txt

output.txt
------------------------------------------------
Oh,  little  prince!  Bit  by  bit  I   came    to
understand the secrets of your sad little  life  .
. . For a  long  time  you  had  found  your  only
entertainment in the quiet pleasure of looking  at
the sunset. I  learned  that  new  detail  on  the
morning of the fourth day, when you said to me:
"I am very fond of sunsets. Come, let us  go  look
at a sunset now."
"But we must wait," I said.
"Wait? For what?"
"For the sunset. We must wait until it is time."
At first you seemed to  be  very  much  surprised.
And then you laughed to yourself. You said to  me:

"I am always thinking that I am at home!"
Just so. Everybody knows that when it is  noon  in
the United States the sun is setting over  France.

If you could fly to  France  in  one  minute,  you
could go straight  into  the  sunset,  right  from
noon. Unfortunately, France is too  far  away  for
that. But on your tiny planet, my  little  prince,
all you need do is move your chair  a  few  steps.
You can see the day end and the  twilight  falling
whenever you like . . .
"One day," you said  to  me,  "I  saw  the  sunset
forty-four times!"
And a little later you added:
"You know--one loves the sunset, when  one  is  so
sad . . ."
"Were you so sad, then?" I asked, "on the  day  of
the forty-four sunsets?"
But the little prince made no reply.

from the Little Prince by Antoine de Saint Exupry

Kaynak Kod:#include <stdio.h>
#include <conio.h>
#include <string.h>


#define LINE_SIZE 50

void to_file(FILE *f, char *str);
char *rtrim(char *str);
char *get_word(FILE *f, char *str);

int main(void)
{
  FILE *in_file;
  FILE *out_file;
  char in_file_name[80] = "hip.txt";
  char out_file_name[80] = "hop.txt";
  char buf[255] = {'\0'};
  char word[255] = {'\0'};

  printf("input file: ");
  gets(in_file_name);
  in_file = fopen(in_file_name, "r");
  if (in_file == NULL) {
     printf("can not open file: %s\n", in_file_name);
     return 0;
  }

  printf("output file: ");
  gets(out_file_name);
  out_file = fopen(out_file_name, "w");
  if (out_file == NULL) {
     printf("can not open file: %s\n", out_file_name);
     return 0;
  }

  while (get_word(in_file, word) != NULL) {
     if (strlen(buf) + strlen(word) > LINE_SIZE)
        to_file(out_file, buf);
     strcat(buf, word);
     while (buf[strlen(buf) - 1] == '\n')
        to_file(out_file, buf);
  }
  return 0;
}
/************************************/
char *get_word(FILE *f, char *str)
{
  int ch, i = 0;
  char *p = str;

  ch = fgetc(f);
  while (ch != EOF) {
     *p++ = ch;
     if (ch == ' ' || ch == '\n' || ++i > LINE_SIZE)
        break;
     ch = fgetc(f);
  }
  *p = '\0';
  if (*str == '\0')
     return NULL;
  return str;
}
/*************************************/
void to_file(FILE *f, char *buf)
{
  char residue[255] = "";
  char *p, *q;
  int flag;

  if (*buf == '\0')
     return;

  if (strlen(buf) <= LINE_SIZE && buf[strlen(buf) - 1] == '\n') {
     fputs(buf, f);
     *buf = '\0';
     return;
  }

  if (strlen(buf) > LINE_SIZE) {
     p = buf + LINE_SIZE;
     q = residue;
     while (*p != '\0')
        *q++ = *p++;
     *q = '\0';
     buf[LINE_SIZE] = '\0';
  }
  else {
     rtrim(buf);
     p = buf + strlen(buf) - 1;
     flag = 0;
     while (strlen(buf) < LINE_SIZE) {
        if (*p == ' ') {
           q = buf + strlen(buf) + 1;
           while (p < q) {
              *q = *(q - 1);
              q--;
              flag = 1;
           }
        }
        p--;
        if (p == buf)
           if (flag == 1)
              p = buf + strlen(buf) - 1;
           else
              break;
     }
  }

  fputs(buf, f);
  fputc('\n', f);
  strcpy(buf, residue);
  return;
}
/*************************************/
char *rtrim(char *str)
{
  char *p = str + strlen(str) - 1;

  while (*p == ' ')
     *p-- = '\0';
  return str;
}

nepaizen
19-04-2006, 22:50   |  #2  
Yeni Üye
Teşekkür Sayısı: 0
24 mesaj
Kayıt Tarihi:Kayıt: Mar 2006

çok kısa bir şekilde fizilsel alandaki bilgiyi belleğe alayım:

#include <iostream>
#include <fstream>


char dosya_adi[] = "xxx.xyz";


void main() {
    std::ifstream fin(dosya_adi, std::ios::binary);
    if(!fin) return;

    fin.seekg(0, std::ios::end);
    unsigned int buffer_size = fin.tellg();
    fin.seekg(0, std::ios::beg);

    char *buffer = new char[buffer_size+1];
    fin.read(buffer, buffer_size);
    fin.close;

    buffer[buffer_len] = '\0';

    std::cout << buffer << std::endl;

    delete [] buffer;
}

Son Düzenleme: nepaizen ~ 19 Nisan 2006 22:51