Thread: Problem with writing

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    29

    Problem with writing

    I made program from reading from one file and then if price of some product is bigger than typed number to write in other file, but it didnt write it.

    Code:
    #include <stdio.h>int main()
    {
        char st[3],nam[15],nmb[4],pr[5];
        int asd,i;
        char ch;
        int l=0;
        FILE *fp,*ft;
        fp=fopen("goods.txt","r");
        if(fp==NULL)
        {
            printf("Error!");
        }
        fscanf(fp,"%s %s %s %s\n",st,nam,nmb,pr);
        printf("Enter number:");
        scanf("%d",&asd);
        ft=fopen("enter.txt","w");
         while((ch=fgetc(fp))!=EOF)
         {
          if (ch=='\n') { l++; }
         }
         int as=atoi(pr);
         for(i=0; i<l; i++)
         {
             if(asd>pr)
             {
                 fputs(nam, ft);
             }
            }   
        fclose(fp);
        fclose(ft);
    }
    EX:
    file1:
    3 ASD 77 15000
    4 BSAD 77 3000
    If J put number 3600 in second file need to write me this:
    BASD

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I'm looking over the code in general. I'm going to point out what I see and ask you some questions so we get to a working program.
    Code:
        fp=fopen("goods.txt","r");
        if(fp==NULL)
        {
            printf("Error!");
        }
    I would also return from main if this failed. You cannot proceed if the file is missing. fclose(NULL); - which is what your code would be doing later, for instance, doesn't work.
    Code:
     char st[3],nam[15],nmb[4],pr[5];
        fscanf(fp,"%s %s %s %s\n",st,nam,nmb,pr);
    You're using fscanf() like all it can do is read strings, only to do something like this later.
    Code:
    int as=atoi(pr);
    This code is not really ideal.

    There's also this code:
    Code:
         while((ch=fgetc(fp))!=EOF)
         {
          if (ch=='\n') { l++; }
         }
    What do you think this is doing?

    Is your goal in the end to have a list of items, sorted by the number in the last column?

  3. #3
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    My goal is to have name of items that have bigger price value than entered number.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    And what is price value? Because right now, your data runs contrary to your expectation. Look:

    3 ASD 77 15000
    4 BSAD 77 3000

    Item ASD is bigger than 3600, not BSAD.

    My own tinkering confirms this result for the goods file you gave.
    Code:
    #include <stdio.h>
    
    int main()
    {
        char itemDesc[32] = "";
        long price = 0;
        long enteredPrice = 0;
        int c;
        FILE *fp = NULL;
        
        if ((fp = fopen("goods.txt", "r")) == NULL)
        {
            fprintf(stderr, "Couldn't open goods.txt file!\n");
            return 1;
        }
        
        printf("Enter price: ");
        while (scanf("%ld", &enteredPrice) != 1) 
        {
            printf("Price not understood. Try entering a number again.\n");
            while ((c = getchar()) != '\n' && c != EOF) 
                /* nothing */;
        }
        
        /* Note that for scanf functions, a * means that the item is not assigned.
           http://linux.die.net/man/3/scanf 
        */
        while (fscanf(fp, "%*d %31s %*d %ld", itemDesc, &price) == 2)
        {
            if (price > enteredPrice) 
            {
                puts(itemDesc);
            }
        }
        fclose(fp);
        return 0;
    }
    
    /* my output
    
    Enter price: 3600                                                                                                                                                       
    ASD 
    */
    That is about as simple as this task gets, though. You might need to do some edits to get it to behave exactly how you want.
    Last edited by whiteflags; 12-26-2015 at 05:02 PM.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    I make this but just working for one items, how to fix it to work for all items ?
    Code:
    #include <stdio.h>int main()
    {
    	char st[3],nam[15],nmb[4],pr[5];
    	int asd,i;
    	char ch;
    	int l=0;
    	int c=0;
    	FILE *fp,*ft;
    	fp=fopen("items.txt","r");
    	if(fp==NULL)
    	{
    		printf("Error!");
    	}
    	fscanf(fp,"%s %s %s %s\n",st,nam,nmb,pr);
    	printf("Enter number:");
    	scanf("%d",&asd);
    	ft=fopen("enter.txt","w");
         while((ch=fgetc(fp))!=EOF)
         {
          if (ch=='\n') { l++; }
         }
         int as=atoi(pr);
    	 while(c<l)
    	 {
    	 	if(as>asd)
    	 	{
    	 		fputs(nam, ft);
    	 		c++;
    		 }
    		}  
    	fclose(fp);
    	fclose(ft);
    }

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Use a loop to repeat fscanf() and the statements that depend on that data, like in the example program I posted.

  7. #7
    Registered User
    Join Date
    Dec 2015
    Posts
    29
    Code:
     while(fscanf(fp, "%*d %31s %*d %ld", itemDesc, &price)== 2)
    I don't understand what ==2 mean can you explain that part ?

    EDIT!!!
    J try this but didn't working and give me one warning:comparison between pointer and integer.
    Code:
    #include <stdio.h>int main()
    {
        char st[3],nam[15],nmb[4],pr[5];
        int asd,i;
        char ch;
        int l=0;
        int c=0;
        FILE *fp,*ft;
        fp=fopen("items.txt","r");
        if(fp==NULL)
        {
            printf("Error!");
        }
        fscanf(fp,"%s %s %s %s\n",st,nam,nmb,pr);
        printf("Enter number:");
        scanf("%d",&asd);
        ft=fopen("enter.txt","w");
        while(fscanf(fp,"%s %s %s %s\n",st,nam,nmb,pr) == 2)
        {
            if(pr>asd)
            {
                fputs(nam, ft);
            }
        }
        fclose(fp);
        fclose(ft);
    }
    Last edited by John5; 12-27-2015 at 06:48 PM. Reason: New info

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    I don't understand what ==2 mean can you explain that part ?
    In at least two places, I've linked the manual page. It explains everything about what I was doing. I hoped you would read it by now. It's big though... so I understand why you didn't.

    Anyway, there are two parts of that document which really matter to the code.
    Code:
        /* Note that for scanf functions, a * means that the item is not assigned.
           http://linux.die.net/man/3/scanf 
        */
        while (fscanf(fp, "%*d %31s %*d %ld", itemDesc, &price) == 2)
    To understand that comment, there is a section that reads as follows (emphasis mine):
    Each conversion specification in format begins with either the character '%' [...] followed by:
    • An optional '*' assignment-suppression character: scanf() reads input as directed by the conversion specification, but discards the input. No corresponding pointer argument is required, and this specification is not included in the count of successful assignments returned by scanf().
    Hopefully, that is mostly clear. Since there are %*d parts to the format string, those inputs are read, and then discarded. I then added two pointers to the argument list. This is where the == 2 comparison came from.

    To explain that in detail, you need to read another part of the manual:
    Return Value

    These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.
    You know what fscanf() is going to return by counting the arguments, excluding the FILE pointer and the format string. When you write a different fscanf() function call from mine, the return value you should expect to compare with will also be different.

    The big idea is that fscanf() is going to scan the file, grabbing 2 specific columns from every line, until there are no more lines.

    J try this but didn't working and give me one warning:comparison between pointer and integer.
    I think you mean to use I to refer to yourself, but type J instead.

    Anyway, it doesn't matter because your programs are still small, but when you post about errors, at least include the line number. It helps everyone know where to look for the problem.
    Code:
    main.c:22:14: warning: comparison between pointer and integer                                                                                                           
    
             if(pr>asd)                                                                                                                                                     
    
                  ^
    The error may even include code to explicitly point out the error. pr is a string, and asd is an integer. These two types cannot be compared in C.
    You would need to, at the very least, call atoi() on pr again for the sake of that comparison. This goes back to my advice about using fscanf() earlier in the thread. You're using it to read strings when you want to compare integers. You can ask fscanf() to do the conversions on the input for you, and that makes writing the program a lot easier.
    Last edited by whiteflags; 12-27-2015 at 08:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem with writing to a file
    By ratnakar in forum C Programming
    Replies: 10
    Last Post: 02-21-2013, 10:43 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 10:47 AM
  3. Problem writing to a file!!
    By cs02407 in forum C Programming
    Replies: 2
    Last Post: 07-17-2007, 12:22 AM
  4. problem with writing to file
    By Welshy in forum C Programming
    Replies: 3
    Last Post: 03-12-2006, 10:24 AM
  5. Problem writing to file
    By HAssan in forum C Programming
    Replies: 7
    Last Post: 11-05-2005, 09:59 PM