So I have to read data from a text file into a [50][50] array and Im not really sure which commands I can use to do that and to write back some data into another file. Would really appreciate some help on this.

Recommended Answers

All 3 Replies

fstreams. Also google for fstream tutorials. ifstream is for input files, ofstream for output files. With a little practice its not all that hard to do. Exactly how to do it will depend on file contents -- post a couple lines from the file.

I tried using the fopen() function to open the file and then subsequently read from it. However, for some reason it doesnt seem to be accepting the filename parameter and it directly jumps to the else portion of the code. Any ideas why?

else if(choice==1)
		{
			printf("\n Enter file name:");			
			scanf("%s",&filename);
			printf("File name entered: %s",&filename);
			p = fopen(&filename,"rt");
			if(p!= NULL)
			{
				int breakcounter=0; // to break the outer for loop when the inner loop breaks.
				for( i=0;i<50;i++)
				{
					for( j=0;j<50;j++)
					{
						if(!feof(p))
						{
							char c=fgetc(p);
							Matrix[i][j]=c;
						}
						
						else 
						{
							breakcounter=1;
							break;
						}
			
					}

					if(breakcounter==1) // to break the outer loop once the inner loop has broken
					{
						break;
					}
				}
			}

			else
			{
				printf("\nThe file name you entered is incorrect. You will now be returned to the main menu.");
			}
		}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.