Thread: fprintf to text file

  1. #1
    Registered User Ray Schmidt's Avatar
    Join Date
    Feb 2003
    Posts
    17

    Question fprintf to text file

    Ok, I've got a text file say like below:

    Imperial Imperial
    1 0001010000000000000000000000 5000
    0 0
    ROOF BASICS
    22 96.000000 0.463648 0.000000 12.000000 12.000000 1 1 1
    4.163119 4.163119 3.500000 3.500000 3.500000 3.500000
    0.250000 0.250000 24.000000
    0.000000 0.000000


    Two things are causing me a problem. I'm not sure if the problem is being caused by my \n or what but if anyone can offer any suggestions, that would be great.

    1) If I change the 22 to 3 in line five, I close the file and wind up with 32. How can I get rid of the 2 and truncate the remainding text to allow only one space between data.
    2) When I modify line six, I end up with line seven missing 0 in 0.2500000 (looking like .2500000)

    Thanks,
    Ray Schmidt


    Code:
    void CPeekaViewDlg::OnSave() 
    {
    	UpdateData(TRUE);
    	
    	FILE *stream;
    	char s[255];
    	char file[100];
    	char getl[100],getl2[100];
    	int i=74,skipper=0,z=0,hb=0;
    
    	//select & open file
    	if(m_quote == 0)
    	{
    		// Quote Directory
    		sprintf(file,"c:/mydocu~1/programming/peekaview/%s.tre",m_truss);
    		//sprintf(file,"j:/mitek/quote/%s/%s.tre",m_job,m_truss);
    		if( (stream  = fopen(file,"r+")) == NULL )
    		{
    			MessageBox("    Trusses Not Found!\nFill In The Information Below", "Error", MB_ICONEXCLAMATION);
    			return;
    		}	
    	}
    	if(m_quote == 1)
    	{
    		// Production Directory
    		sprintf(file,"c:/mydocu~1/programming/peekaview/debug/%s.tre",m_truss);
    		//sprintf(file,"j:/mitek/prod/%s/%s.tre",m_job,m_truss);
    		if( (stream  = fopen(file,"r+")) == NULL )
    		{
    			MessageBox("    Trusses Not Found!\nFill In The Information Below", "Error", MB_ICONEXCLAMATION);
    			return;
    		}	
    	}
    	
    	
    	//quanity
    	fseek(stream, i, SEEK_SET);
    	fscanf(stream,"%s",s);
    	skipper = strlen(s);
    	z = i + skipper + 1;
    	fseek(stream, z, SEEK_SET);
    	fgets(getl,55,stream);
    	fseek(stream, z+56, SEEK_SET);
    	fgets(getl2,54,stream);
    	m_qty == &m_changeqty;
    	i = 75;
    	fseek(stream, i, SEEK_SET);
    	if(strlen(m_changeqty)==1)
    	{
    		fprintf(stream, "%s", m_qty);
    	}
    	if(strlen(m_changeqty)>1)
    	{
    		fprintf(stream, "%s%s\n", m_qty, getl);
    		fseek(stream, z+55, SEEK_SET);
    		fprintf(stream, "\n%s\n", getl2);
    	}
    	if(strlen(m_changeqty)>2)
    	{
    		fprintf(stream, "%s%s\n", m_qty, getl);
    	}
    	
    	fclose(stream);
    	m_workc = getl2;	
    	UpdateData(FALSE);
    }

  2. #2
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    You are trying to process the file as a binary file, but the format of the file is sequential text. There is another thread from recently that goes over processing a file in binary...I wont rehash it here. I think the thread is called "Deleting/Searching Directly File I/O", it is about a page back if you look for it.

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    here is a link to the mentioned thread:

    http://cboard.cprogramming.com/showt...threadid=34807

    There is a wealth of file i/o information here on the boards, a quick search may prove to be all you need.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM