Thread: Help with C++ file input/output

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    3

    Help with C++ file input/output

    I'm rather new at C and this is the code that I have:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    
    
    void invoiceFile();
    
    
    
    int main(void){
    invoiceFile();
    return 0;
    }
    
    
    
    
    
    void invoiceFile(){
    FILE *ordPtr;
    ordPtr = fopen("orders.txt", "r");
    FILE *invPtr;
    invPtr = fopen("inventory.txt", "r");
    while(!feof(invPtr)){
    	int n,  addNum, zip, itemselect, quantity;
    	char x[28];
    	int item;
    	char itemName[28], firstName[28], lastName[28], address1[28], st[28], city[28], state[28];
    	int cost, inventory;
    	fscanf(invPtr, "&#37;d, %s, %d, %d", item, itemName, cost, inventory);
    	srand(time(NULL));
    	n = rand () % 1000;
    	
    		while(!feof(ordPtr)){
    			fscanf(ordPtr, "%d %s %s %d %s %s %s %s %d %d", itemselect, firstName, lastName, addNum, address1, st, city, state, zip, quantity);
    			if(itemselect == item){ 
    				int invPtr;	
    				sprintf(x, "invoice%d.txt", n);
    				FILE *invoice;
    				invoice = fopen(x, "w");			
    				fprintf(invoice, "Invoice for:\n%s %s\n%d %s %s\n%s %s %d\n\nItem#   product              qty   price each\n%-8d%-21s%-6d%-10d\n                     total = $%d\n", firstName, lastName, addNum, address1, st, city, state, zip, item, itemName, quantity, cost, quantity*cost);
    			}
    		}
    	}
    }
    It's a work in progress, so style will be updated, etc. and there is more to flesh out the code. However, I am getting a segmentation fault and I couldn't pinpoint exactly where it was since I'm unfamiliar with the syntax and using/creating files. The idea is that it reads from an inventory.txt file and an order.txt file and compiles an invoice. Each invoice file will have a unique and random name.

    Here's the inventory.txt:

    200 CD-player 50 9
    400 television 300 4
    600 surround-sound 120 7
    800 DVD-player 75 2
    1000 beta-max 15 75
    1200 8-track-player 25 100
    1400 mp3-player 100 15
    1600 boombox 150 25
    1800 alarm-clock 50 15
    2000 universal-remote 35 12


    orders.txt:

    200 CD-player 50 9
    400 television 300 4
    600 surround-sound 120 7
    800 DVD-player 75 2
    1000 beta-max 15 75
    1200 8-track-player 25 100
    1400 mp3-player 100 15
    1600 boombox 150 25
    1800 alarm-clock 50 15
    2000 universal-remote 35 12

    and here's just an example of what the invoice file should look like:
    Code:
    Invoice for:
    Justin Time
    2121 Green Rd
    Mediumtown, IA 21345
    
    item#	product	qty 	price each
    400	television	3	$300
    			 total = $900
    Thank you for your help or any advice!
    Last edited by Rat; 04-14-2008 at 10:37 PM.

  2. #2
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    >>I'm rather new at C++ and this is the code that I have:

    However what you have is C and not C++. Also, I don't get a seg-fault, it flat out doesn't compile.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Oh, yeah...sorry this one is in c...

    Did you create an inventory.txt and orders.txt file? Because I have no problem compiling it and that's all I can come up with.
    Thanks, though, for trying it out.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> However what you have is C and not C++.

    to be sure, using c-runtime functions doesn't make it any less of a c++ program.
    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;
    }

  5. #5
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Did you create an inventory.txt and orders.txt file?
    yes.

    Oh hold on, just warnings.

    Compiler is set to level = paranoia

    The problem lies here:
    Code:
    fscanf(ordPtr, "%d %s %s %d %s %s %s %s %d %d", itemselect, firstName, lastName, addNum, address1, st, city, state, zip, quantity);
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    3
    Ah, thank you.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. File being filled with NULLs
    By Tigers! in forum Windows Programming
    Replies: 2
    Last Post: 06-30-2009, 05:28 PM
  3. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM