Thread: C - Reading first 20 charecters from file

  1. #1
    ThoughtRiot
    Guest

    C - Reading first 20 charecters from file

    Is there a way (and how) to read the first 20 (1-20) charecters from a file, store it, then read the next 20 charecters (21-41), store it, and so on?

    OR

    Is there a way to read and store all the charecters (content) of a file into an array, then divide it into segments of 20 charecters each either in the array or into other variables?

    I am using the Win LCC C Compiler.

    Thanks For Bothering To Read This

  2. #2
    ThoughtRiot
    Guest

    ?

    that only works for the first 20 charecters, now how do i get to the next 20 with out reading also the first 20 (40)

  3. #3
    ThoughtRiot
    Guest

    Oh

    thanks then, i never actualy used fread so...

    thanks both of you.
    ill let you knwo if it helped.

  4. #4
    ThoughtRiot
    Guest

    ?

    1245020 <- thats the number i got outputed to the screen with this:

    Code:
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    char buf[20];
    int  n;
    FILE *fd;
    fd = fopen("new.txt", "r");
    	if( fd ) {
    		while((n = fread(buf, sizeof(char), 20, fd)) > 0) {
        /* do something with buf, which contains n read bytes */
    		printf("%d", &buf);
      		}
    	}
    return 0;
    }
    i also tried 'printf("%d", &n);'

    it always outputs that number??? even though the file does not have that written in it?

  5. #5
    Registered User angeljicu's Avatar
    Join Date
    Nov 2002
    Posts
    16

    Question I don't know why?

    i tried,found some wrong

    #include<stdio.h>
    main()
    {
    char *string;
    FILE *fp;
    clrscr();
    fp=fopen("c:\\file.txt","r");
    while(fread(string,sizeof(char),20,fp));
    printf("%s",string);
    fclose(fp);
    }

    run :the screen display:

    aksdjfklsajdgkljdsak¨g♣©¤♣v♠¦£♠ⁿ♠=Tb¨º2 k &aring; ¨m ¨{ ⌠
    ¡ý
    (
    <
    O
    a
    x
    &Aring;

    if I change "char *string;" to "char string[20];"
    the result:
    aksdjfklsajdgkljdsak¦¸ ↔☺☺

    I don't know why?
    there is angel,who love to see u.

  6. #6
    ThoughtRiot
    Guest

    ? again

    heres what I compiled:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
    char buf[20];
    int  n;
    FILE *fd;
    fd = fopen("new.txt", "r");
    	if(fd) {
    		while((n = fread(buf, sizeof(char), 20, fd)) > 0) {
        /* do something with buf, which contains n read bytes */
    		printf("%.20s", buf);
      		}
    	}
    return 0;
    }
    it outputed the contents of the file (asdfasdfasdfasdf) like this:

    asdfasdfasdfasdf- ZZ-

    ??

  7. #7
    ThoughtRiot
    Guest

    Der

    Sorry bout that, momment of stupidity, i only had (14, i think) 14 charecters in 'new.txt', pfft. heh.

    thank you verry much for you help, i appreciate it.

  8. #8
    Registered User angeljicu's Avatar
    Join Date
    Nov 2002
    Posts
    16

    as you say......

    as you say,i just knew,thank you all.
    there is angel,who love to see u.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM