0

I have a program which writes to text file.

Example in my textfile:

4 QA131 100 100 100 100 QA132 100 100 100 100 QA133 100 100 100 100 QA134 100 100 100 100 

I want to write a new line which store the record and the 4 will turn into 5.

What is the method used to write to the file?

Here is my code:

void inputRecord() { Product product[101]; char FILENAME[20]; cout<<"Enter the file you want to open: "; cin>>FILENAME; ifstream read(FILENAME); if (read.is_open()) { while(read.good()) { read>>product[0].line; for(int i =0;i< product[0].line;i++) { read >>product[i].PartNumber >>product[i].initialQuantity >>product[i].quantitySold >>product[i].minQuantity >>product[i].price; } product[0].line+=1; } read.close(); } ofstream write(FILENAME); write<<product[0].line << endl; for(int i =0;i< product[0].line;i++) { write << product[i].PartNumber << '\t' << product[i].initialQuantity << '\t' << product[i].quantitySold << '\t' << product[i].minQuantity << '\t' << product[i].price << '\t' << endl; } write.close(); cout << "Inventory added successfully!" << endl; system("pause"); system("CLS"); } 

I cannot write the item to the file.

7
  • I'm curious why you're writing out the entire file every time. I think it would be more efficient to append the new Product to the file, and remove the count line. Get the count inside your while{} loop, incrementing your counter with each successful record read until you reach End Of File. Commented Sep 12, 2013 at 15:18
  • You haven't posted the part of your program where you add the new item to the product array. Is that the part you are stuck on? Commented Sep 12, 2013 at 15:18
  • @john: I noticed that too. Based on the array declared size of 0 to 100, I'm assuming that adding a new item to the array is not the problem. But one never knows. Commented Sep 12, 2013 at 15:20
  • Question for the poster: When you say you cannot write the item to the file, what error message are you getting? Commented Sep 12, 2013 at 15:21
  • Hi buddies, sorry for misunderstood u all. My issue: no error message, i can write into file. but i cannt to input a new line. Commented Sep 12, 2013 at 15:26

1 Answer 1

2

you could just use fseek to solve your problem.

Here's a really simple example based this C++ reference example

#include<cstdio> int main(void) { FILE * pFile; pFile = fopen ( "test.txt" , "wb" ); fprintf(pFile,"00000\n"); // watch spacing for (int i=1; i<11; i++) { fprintf (pFile, "%d. This line a line of stuff.\n",i); } fseek(pFile , 0 , SEEK_SET ); fprintf(pFile,"10 \n"); fclose(pFile); return 0; } 
Sign up to request clarification or add additional context in comments.

2 Comments

@user2747285 fseek can be used in any file opened in binary mode. There's nothing to stop you writing text to a file opened in binary mode if that's what you want to do.
glad it helped. Please mark it as answered if that's the approach you used.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.