looping issue

This is a discussion on looping issue within the C Programming forums, part of the General Programming Boards category; hi all i am trying to extract contents from a file, the content is the make up for other files ...

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

    looping issue

    hi all

    i am trying to extract contents from a file, the content is the make up for other files e.g. the file would contain something like:
    file1Rfile contents\n
    Qfile2Rhello world\n
    Q

    now up until R is the file name and up until Q is the file contents

    i am just having a small issue on the fact that i cannot get my program to move onto the next part in the buffer it just stops at the first file

    i have been told by tabstop that i need a loop along with a way to empty the buffer every time it gets full as it is limited to 64 characters at the moment

    so far i have:
    Code:
    	char heading[64];
          
    	int x,len;
    	len=strlen(buffer);
    	for (x=0; x<=len; x++)
            heading[x]=buffer[x];
    
    	while (heading[j] != '\0')
    	  heading[j++];
    	if (heading[j+1] = 'R')
    	{
    	strncpy (buffera, heading, 5);
    
    	nwrite = open(buffera, O_CREAT, S_IRUSR|S_IWUSR);
    
            while (heading[j] != '\0')
    	  j++;
    	if (heading[j+1] = 'Q')
    	{  
    	outfiledbody = open(buffera, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR|S_IWUSR);
    	strncpy (bufferb, &heading[j], 5);
    	nwritebody = write(outfiledbody, bufferb, strlen(bufferb));
    	}
    now i know there is alot of work to be done here!
    where ive put strncpy (buffera, heading, 5); i dont mean for it to be only 5 characters that is for test purposes for now

    with this code j for some reason equals 64, i have no idea how j became 64 although heading has 64 characters

    i am hoping that this will get looping around properly so it can move on and create the second file

    the previous code i used worked but only on the first file this is what that looked like:

    Code:
    	char heading[64];
          
    	int x,len;
    	len=strlen(buffer);
    	for (x=0; x<=len; x++)
            heading[x]=buffer[x];
    
    	char *pos = strchr(heading, 'R');
    	char *pos2 = strchr(heading, 'Q');
    	int posa = pos-heading;
    	int pos2a = pos2-heading;
    	int pos2b = pos2a - posa;
    
            if (pos)
            {
    	strncpy (buffera, heading, posa);
    	buffera[posa] = '\0';
    	nwrite = open(buffera, O_CREAT, S_IRUSR|S_IWUSR);
    	
            outfiledbody = open(buffera, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR|S_IWUSR);
    	strncpy (bufferb, &heading[posa+1], pos2b);
    	bufferb[pos2b-1]='\0';
    	nwritebody = write(outfiledbody, bufferb, strlen(bufferb));
    	}

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by aadil7 View Post
    hi all

    i am trying to extract contents from a file, the content is the make up for other files e.g. the file would contain something like:
    file1Rfile contents\n
    Qfile2Rhello world\n
    Q
    Have you considered what happens if your file actually contains an R or a Q?

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    yep ive been through that and i have problems with ascii at home but i will change that

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    I see a couple issues. One relates to brace placement.
    Code:
    	if (heading[j+1] = 'R')
    	{
    	strncpy (buffera, heading, 5);
    
    	nwrite = open(buffera, O_CREAT, S_IRUSR|S_IWUSR);
    
            while (heading[j] != '\0')
    	  j++;
    	if (heading[j+1] = 'Q')
    	{  
    	outfiledbody = open(buffera, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR|S_IWUSR);
    	strncpy (bufferb, &heading[j], 5);
    	nwritebody = write(outfiledbody, bufferb, strlen(bufferb));
    	}
    AFAICT, the second if is dependent on the first if, because you've written essentially this:
    Code:
    if ( heading[x] == 'Q' ) {
       ....
       if ( heading[x] == 'R' ) {
       ...
    }
    And how can that be right? It shouldn't even compile.

    The second problem relates to the area in blue. The blue is a proper comparison, whereas what you wrote in your post is an assignment. In an if statement, this mistake would elicit a warning from any c compiler on the planet. I don't even think warnings need to be high to get the compiler to notice this mistake.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    ow no sorry but that was just a code problem when i pasted it on here
    i have actually closed the brackets before the next while

    and im not actually sure what part of the code you are pointing to when it comes to the '=='

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,750
    and im not actually sure what part of the code you are pointing to when it comes to the '=='
    Observe again what you wrote here:
    Code:
    if (heading[j+1] = 'R')
    	{
    	strncpy (buffera, heading, 5);
    
    	nwrite = open(buffera, O_CREAT, S_IRUSR|S_IWUSR);
    
            while (heading[j] != '\0')
    	  j++;
    	if (heading[j+1] = 'Q')
    	{  
    	outfiledbody = open(buffera, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR|S_IWUSR);
    	strncpy (bufferb, &heading[j], 5);
    	nwritebody = write(outfiledbody, bufferb, strlen(bufferb));
    	}
    You are using an assignment statement in the condition part of an if. C compilers warn about this. The 'R' or 'Q' would be assigned to header[x] (or whatever it actually is), and the header[x] is true unless it is zero.

    So this

    if ( header[x] = 'Q' )

    does this

    if ( ( header[x] = 'Q' ) != '\0' )

    I'd be surprised if you came back and said that is what you meant to do.

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    Code:
    if (heading[j+1] = 'R')
    	{
    	strncpy (buffera, heading, 5);
    
    	nwrite = open(buffera, O_CREAT, S_IRUSR|S_IWUSR);
            }
            while (heading[j] != '\0')
    	  j++;
    	if (heading[j+1] = 'Q')
    	{  
    	outfiledbody = open(buffera, O_CREAT | O_WRONLY | O_APPEND, S_IRUSR|S_IWUSR);
    	strncpy (bufferb, &heading[j], 5);
    	nwritebody = write(outfiledbody, bufferb, strlen(bufferb));
    	}
    hmm well i can tell you that what that piece of coding is meant to do is check through the buffer byte by byte and check for NULL. once it has found NULL and the next character (heading[j+1]) is Q then carry on with the process

  8. #8
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,652
    = for assignment, == for comparison.

  9. #9
    Registered User
    Join Date
    Mar 2010
    Posts
    85
    im just really confused on my way out of this mud im stuck in

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    	for (x=0; x<=len; x++)
            heading[x]=buffer[x];
    
    	while (heading[j] != '\0')
    	  heading[j++];
    	if (heading[j+1] = 'R')
    So actually look at what this code really does. len is gotten from strlen, so it is the number of characters before the first \0 character (should one exist, which there's no guarantee of that unless you chose a read-from-file function that guarantees a \0 at the end of input). Therefore the while loop is completely pointless; it will set j to len. You then try to check heading[j+1]. It is flat out guaranteed that you have not touched heading[j+1]; it is not possible for this spot in the array to contain valid data. Therefore this check cannot possibly do what you want (the body of the if happens anyway, since as pointed out above you are incorrectly using = instead of ==).

    The thing you need to realize is that you need to process the file, which does not involve searching for a Q or an R in any way, shape, or form. You need to go through, character by character, and examine the character and decide what to do with it. You will start out (it looks like) by adding those characters to a filename. When you come across a character that starts a special sequence for you, then you can temporarily stop and see whether you have the whole special sequence. If so, then you finalize your filename, and start in on phase II: reading the contents of the file. You need to process these contents, probably by re-writing them to a new file (on the assumption we're in extract mode here). You can't just try searching for stuff -- you have to deal with it as it comes in.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Do-While looping issue
    By atom.sk in forum C Programming
    Replies: 7
    Last Post: 12-19-2010, 02:02 PM
  2. Getlines and looping
    By Blanked in forum C++ Programming
    Replies: 5
    Last Post: 12-01-2009, 07:26 PM
  3. float calculation issue
    By George2 in forum C# Programming
    Replies: 1
    Last Post: 05-26-2008, 05:56 AM
  4. Looping Issue
    By stormy in forum C Programming
    Replies: 5
    Last Post: 01-24-2006, 02:35 AM
  5. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21