Thread: help withcreating sequential text file

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    it tells errors as i change printf(too few arguments)
    ... one must be a master in mind reading to give answers here==(

    You probably forgot the FILE argument when using fprintf(), but I lack the mind reading skills to be sure in the absence of the code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User
    Join Date
    May 2008
    Posts
    72
    where should i insert it

  3. #18
    Registered User
    Join Date
    May 2008
    Posts
    72
    exuse for lack of understanding

  4. #19
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    where should i insert it
    If you have to ask that, then you have no clue as to what your program is doing.

    exuse for lack of understanding
    It's okay, but let's get you started on file handling. Put aside your current work for the time being and write a program that reads in some text of no more than 14 characters from the user and writes the text to a file (pick a filename of your choice) using fprintf().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #20
    Registered User
    Join Date
    May 2008
    Posts
    72
    i'll just let it for now it just that the instructor did only the lectur with no exemple of an implementation using fprintf and stuff. I'll make my mind later

  6. #21

  7. #22
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The bottom line is you need to have all of the relevent API documentation handy before you start coding. And a little initiative wouldn't hurt either...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  8. #23
    Registered User
    Join Date
    May 2008
    Posts
    72
    thanks for the useful links. So the result that i got is this
    Code:
    #include<stdio.h>
    #include<string.h>
    #define size 5           
    
    
    typedef struct {
    	char last[10];                        /* Structure of type student*/
    	char first[10];              
        double gpa;
    } stud;
    
    
    typedef struct {
    	char last[10];
    	char first[10];                      /* structure of type employee*/
    	double salary;
    }  emp;
    
    
    void check_add_salary (emp *e, stud *s);
    
    
    
    
    main(){ 
    
    FILE *stud_file=NULL;
    FILE *employ_file=NULL;
    FILE *employ_file2=NULL;
    int i=0 , j=0;
    stud arr_stud[size];                                                  /* An array of structures (students)*/
    emp arr_emp[size];                                                    /*An array of structures (employees)*/
                    
    
    if((stud_file=fopen("student.txt","r"))==NULL) 
       printf("\nThe file could not be opened\n\n");
    
    else
       {   fseek(stud_file,50,SEEK_SET);
           while(!feof(stud_file))
       	   {fscanf(stud_file,"&#37;s%s%lf\n", arr_stud[i].last,arr_stud[i].first,&arr_stud[i].gpa);
       
           i++;
       	   }
       
           
           
         
         
          fclose(stud_file);
       }
       /*end of else*/
       
       i=0;
       
       if((employ_file=fopen("employees.txt","r"))==NULL)
    {  
       printf("\nThe file could not be opened\n\n");
    }
    
    else 
       {
         fseek(employ_file,51,SEEK_SET);
       	 while(!feof(stud_file))
    	   	{
       	    fscanf(employ_file,"%s%s%lf\n", arr_emp[i].last,arr_emp[i].first,&arr_emp[i].salary);
     	   
            i++;   
            }
       
       fclose(employ_file);
       
       
       
       
       }
       
       /***end of reading from file employess*/
       
       
       check_salary(arr_emp,arr_stud);
       
       
       
       if((employ_file2=fopen("update.txt","w"))==NULL)
    {  
       printf("\nThe file could not be opened\n\n");
    }
    
    else 
       {  fputs("\t\tThe new list of employees(after salary change)\n\n",employ_file2);
           fputs("Last name\tFirst name\t\tGPA\n",employ_file2);
           fputs("************************************************\n",employ_file2);
           
           for(i=0;i<size;i++)
             fprintf(employ_file2,"%-10s      %-10s               %-10.2f\n", arr_emp[i].last,arr_emp[i].first,arr_emp[i].salary);   /* Display the list of employees sorted by alphabetical order*/
    	   
           fputs("\n************************************************\n\n\n\n",employ_file2);
           fclose(employ_file2);
       
       
       
      
      printf("\n The file of employees with the new salary has been created n\" );
       
    
    
       
    }
       void check_salary (emp *r, stud *s)
       	{
       	
       		int i , j;
            for(i=0;i<size;i++)
              for(j=0;j<size;j++)
                if( strcmp (e[i].last,s[j].last)==0 && strcmp(e[i].first,s[j].first)==0 )
                  if(s[i].gpa>3.0)                                                                                             /* The if statement is to add 10% to the salary*/
                    {																														/*of employees whose students have GPA>3*/
    		        	r[i].salary = r[i].salary + ((r[i].salary)*0.1); j=size-1;
    		     	}       
    			
       }
    but by using fseek its no more sequential . Anyone can tell how to make sequential?

    Thanks

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by overlord21 View Post
    thanks for the useful links. So the result that i got is this but by using fseek its no more sequential . Anyone can tell how to make sequential?

    Thanks
    Don't use fseek? It would seem to be wrong in this context.

  10. #25
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    I think you are confused too much with the sequential thing. The difference is only that in a sequential format you don't have a file pointer that can take any value. You start from the beginning reading until you go to the end. That is what you are doing. Most of the times that is what people do.
    You ONLY fseek() once, do go 50 spaces ahead. That is not random access. If you want, you can read 50 characters and have the same result with 50 times fgetc() which reads one character a time. That will move the file pointer 50 spaces ahead.

    EDIT: I believe the difference with sequential and random access lies on the hardware, not the software. If you had a tape you will have to read from the beginning to the end. In a HD you can read from wherever you want. If you kind of think though how a tape reader would work, it would be fine to use fseek() if the tape reader wasn't too simple. If the tape reader didn't want to use the same functions with files then it would have its own, thus no fseek(). Anyway. Sequential is really not going back to the file. Thus not using fseek() to go back. But in any case, yeah, just not use fseek
    Last edited by C_ntua; 09-24-2008 at 08:16 AM.

  11. #26
    Registered User
    Join Date
    May 2008
    Posts
    72
    i see so the program is fine as it is now right ot should i remove one fseek since i use two?...... and for the " printf("\n The file of employees with the new salary has been created\n\n\");" it tells me that there is some unterminated string or character ..

  12. #27
    Registered User
    Join Date
    May 2008
    Posts
    72
    Quote Originally Posted by C_ntua View Post
    EDIT: I believe the difference with sequential and random access lies on the hardware, not the software. If you had a tape you will have to read from the beginning to the end. In a HD you can read from wherever you want. If you kind of think though how a tape reader would work, it would be fine to use fseek() if the tape reader wasn't too simple. If the tape reader didn't want to use the same functions with files then it would have its own, thus no fseek(). Anyway. Sequential is really not going back to the file. Thus not using fseek() to go back. But in any case, yeah, just not use fseek
    wow with this i'm more confused,ok then if i don't use fseek and want to skip reading the first line of the line how should i do it?

  13. #28
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by overlord21 View Post
    aie! it tells errors as i change printf(too few arguments)
    Code:
    Example:
    #include <stdio.h>
    
    int main(void)
    {
       FILE *stream;
       int i = 100;
       char c = 'C';
       float f = 1.234;
    
       /* open a file for update */
       stream = fopen("DUMMY.FIL", "w+");
    
       /* write some data to the file */
       fprintf(stream, "%d %c %f", i, c, f);
    
       /* close the file */
       fclose(stream);
       return 0;
    }
    A simple example.

  14. #29
    Registered User
    Join Date
    May 2008
    Posts
    72
    thanks and for the little problem in my last post can you help? Adak?

  15. #30
    Registered User
    Join Date
    May 2008
    Posts
    72
    well i removed the fseek and it works

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM

Tags for this Thread