Thread: while loop within a read function

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    85

    while loop within a read function

    PLEASE SCROLL TO THE BOTTOM CODE AS THIS PART IS SOLVED
    THANK YOU


    hi everyone

    i have just done some research and could not find much about the while loop used alongside the read function

    im actually starting to think weather this can be done for my situation because i would like to open a file and read what is in the file up to a special character indicator.
    i would like to then store what has been read, but the while loop would read each individual character and then pass it into storage, therefore overwriting the previous character.

    is this method the most efficient way or is there a better solution? can it be done with a while loop or is it better for me to look in the string.h library? (doing this now)

    kind regards all help is sincerely appreciated
    Last edited by aadil7; 12-11-2010 at 09:21 PM. Reason: solved, now problem with code

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ok with regards to the while loop i have found this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      long sum = 1L;
      int j = 1;
      int count = 10;
      int i;
      for(i = 1 ; i <= count ; i++)
      {
        sum = 1L;   
        j=1;        
        printf("\n1");
    
        while(j < i){
          sum += ++j;
          printf("+%d", j);
        }
        printf(" = %ld\n", sum);
      }
      return 0;
    }
    1 = 1

    1+2 = 3

    1+2+3 = 6

    1+2+3+4 = 10

    1+2+3+4+5 = 15

    1+2+3+4+5+6 = 21

    1+2+3+4+5+6+7 = 28

    1+2+3+4+5+6+7+8 = 36

    1+2+3+4+5+6+7+8+9 = 45

    1+2+3+4+5+6+7+8+9+10 = 55
    the above is the result to the code. This shows me that previous numbers can be stored but can previous characters be stored in the same way?

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You are not storing numbers here (unless you mean accumulating the sum as you go).

    If you want to read in a file, and store more than one character of that file, then you need to allow yourself more than one character of storage to put the file into.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    now in string.h i have found three main functions that i think would be useful

    int strlen(const char *string) - help me find string length

    char *strncpy(const char *string1,const char *string2, size_t n) - this is meant to copy n characters of string 2 to string 1, here i could get n from strlen and replace string 2 with the the file i am initially reading from

    char *strchr(const char *string, int c) - this is meant to find the the first occurrence of c, which i can change c to my special character

    my only issue is after its read the first one in the file will it just loop around the same part or will it move onto the next part in in the file?

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ok now im getting spoilt for choice
    i dont know where to start from what functions to use on my program or anything
    i do know ill need the while loop but after that is a mystery because when it loops around i dont want it looping around reading the first part in the file again i want it to move on and read the second part in the file

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by aadil7 View Post
    ok now im getting spoilt for choice
    i dont know where to start from what functions to use on my program or anything
    i do know ill need the while loop but after that is a mystery because when it loops around i dont want it looping around reading the first part in the file again i want it to move on and read the second part in the file
    Do you have any idea about how file i/o works? (Hint: No.) You should probably look more closely at how read() works before you start worrying about this part, then.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    hmm good idea
    im going to be reading into that first

  8. #8
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ok now its 3am and i think im almost there
    i am just getting a very strange error which is strange!

    my special character is "¥"

    Code:
    int nread;
    char buffer[64];
    
    nread=read(infiled, buffer, buffer != "¥");
    
    nwrite = open(buffer, O_CREAT, S_IRUSR|S_IWUSR);
    ok now what happens is until it hits ¥ it copies everything into buffer which is represented by nread

    when i did the open statement it didnt let me put nread as it was an integer so i just put buffer and it made the files but their names were nowhere near what they were supposed to be (they were along the lines of f��)

    now im not entirely sure on what to do because i thought i had this figured out
    could someone show me the light please

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    nread=read(infiled, buffer, buffer != "¥");
    This is an absolutely stupid thing to do. Look at what read() does again.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ok i agree that was bad

    in the file the first line it reads says file1
    i have got it to write file���
    using:
    nread=read(infiled, buffer, sizeof (buffer == "¥"));
    read file des, store in, how much to store

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Why do you think buffer == "¥" even makes sense? You cannot compare strings with == signs.

    edit to add: And still -- you cannot check for things until you have read them in. You cannot try to use the contents of buffer until after that read function has happened. You cannot use the future to determine how much to read. You need to read in however much you can, limited by the size of the file or the size of the buffer. Only after you have read it in can you look to see whether, and if so where, you have a ¥ character.

  12. #12
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ok so now i have the read complete and the buffer its shows the contents it read.

    my issue is manipulating what the buffer has read so i can use the correct parts of the content to create my file

    when i do any sort of manipulation e.g.:
    Code:
    L43 strcpy(heading[],buffer[])
          {
    	int x,len;
    	len=strlen(buffer);
    	for (x=0; x<=len; x++)
            heading[x]=buffer[x];
          }
    it comes up with a error

    43: error: expected expression before ‘]’ token

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you trying to write your own strcpy? Why? If you are you need to look again at what function declarations are supposed to be.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Histogram function
    By davidjg in forum C Programming
    Replies: 14
    Last Post: 11-24-2010, 12:21 AM
  2. can't compile library
    By pjs111 in forum C Programming
    Replies: 16
    Last Post: 05-24-2010, 01:42 PM
  3. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  4. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM