11 Replies - 4912 Views - Last Post: 07 December 2011 - 07:43 PM Rate Topic: -----

#1 drowningintheC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-December 11

Exporting data to a .txt file in C

Posted 07 December 2011 - 11:03 AM

Okay, so I'm trying to create a program that can take in specific data and store it in a .dat file, which is working fine.
However, I also have to export it to a .txt file. I'm struggling with this. I tried manually creating a file, and that stopped my program from even opening to execute. I tried just writing it to a .txt file instead of a .dat, but every time I try to compile it that way, it comes up with an error. (Specifically, it says ld.exe will not allow the program to execute.)

I'm really new to this, so any help or tips would be very appreciated.

#include <stdio.h>

int main(void)
{
    int recordnumber;
    char toolname[30];
    int quantity;
    double cost;

    FILE *cfPtr;

    if ((cfPtr = fopen("hardware.dat", "w")) == NULL )
    {
        printf("File could not be opened.\n");
    }
    else
    {
        printf("Enter the record number, tool name, quantity, and cost.\n");
        printf("Enter EOF to end input.\n");
        printf("?");
        scanf("%d%s%d%lf", &recordnumber, toolname, &quantity, &cost);

        while (!feof(stdin))
        {
            fprintf(cfPtr, "%d %s %d %.2f\n");
            printf("?");
            scanf("%d%s%d%lf", &recordnumber, toolname, &quantity, &cost);
        }

        fclose(cfPtr);
    }

    return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Exporting data to a .txt file in C

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 11:08 AM

Where are you trying to open a .txt file in this program, I only see you opening a .dat file. There really should be no difference between a .dat and a .txt file, just the extension name. Maybe open a blank text file in some type of notepad, and save it as your .txt file. What might be happening is that the txt file doesn't exist, therefore it cannot be opened.

If you delete the .dat file and try to run this program, does it do the same thing as it did when you tried to open the .txt file? Also, could you post what compiler and OS you're using and any error/warning messages you are receiving.
Was This Post Helpful? 0
  • +
  • -

#3 drowningintheC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-December 11

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 11:35 AM

Sorry, I didn't even consider that. I'm using CodeBlocks with the compiler that came with it on Windows Vista.
I did try opening a blank text file in Notepad and saving it as my file, and it did nothing.

When I deleted the .dat file and ran it, it created a .dat file itself. What I'm not understanding is how to get it to create a .txt file. I tried substituting the "hardware.dat" line with "hardware.txt" but that came up with an error that read:

file ld.exe cannot open output file bin\Debug\InFile.exe Permission denied.

I've been looking online and I don't know if I should have a separate argument for creating a text file (or even writing to the blank text file I created in Notepad) within my main program. I'm sorry if I seem really unintelligent, but....well, I'm really inexperienced.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 12:05 PM

Can you post the code where you try to open a .txt file and it fails?
Was This Post Helpful? 0
  • +
  • -

#5 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 12:11 PM

After going through your code, I just want to ask what you think the code you have is suppose to do. Here is an example of opening/creating a file called "data.txt" and writing "Hello, World!" to it:
// fileTest.c
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
	FILE* fptr = fopen("data.txt", "w");
	
	if(fptr == NULL)
	{
		printf("Error: could not open 'data.txt'\n");
		return EXIT_FAILURE;
	}
	
	fprintf(fptr, "Hello, World!\n");
	fclose(fptr);
	
	return 0;
}


After you compile and run this program, there will be a text file and it will contain "Hello, World!". Let me know if this program doesn't work on your computer because it compiled and ran fine on linux using gcc.

This post has been edited by vividexstance: 07 December 2011 - 12:12 PM

Was This Post Helpful? 0
  • +
  • -

#6 drowningintheC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-December 11

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 12:31 PM

I think I must be misunderstanding what I should be doing. Because I've been running a separate program to try and open the .txt file. My problem is getting the first program to create the text file. So, should I have a separate argument in the code of the first program that creates a .txt file that contains the information in my .dat file? If so, do I create a separate file within my first program? Or should I be trying to start out with a .txt file to begin with?

Just for the sake of clarity, here's the code of the second program.


#include

int main(void)
{
          int recordnumber;
          char toolname[30];
          int quantity;
          double cost;

          FILE *cfPtr;
          
          if ( (cfPtr = open("hardware.txt", "r+") ) ==NULL )
          {
                    printf("File could not be opened.\n"
          }
          else 
          {
                   printf( "%-10s%-13s%-10s%s\n", "Record Number", "Tool Name", "Quantity", "Cost" ); 
                   fscanf( cfPtr, "%d%s%lf", &recordnumber, toolname, &quantity, &cost );
          
                   while ( !feof( cfPtr ) ) 
                   {
                            printf( "%-10d%-13s%-10d%7.2f\n", recordnumber, toolname, quantity, cost ); 
                            fscanf( cfPtr, "%d%s%d%lf", &recordnumber, toolname, &quantity, &cost );
                   }
          
                   fclose(cfPtr);
          }

          return 0;



Sorry about the formatting. Apparently I'm screwing everything up today.

This post has been edited by JackOfAllTrades: 07 December 2011 - 12:52 PM
Reason for edit:: Fixed tags

Was This Post Helpful? 0
  • +
  • -

#7 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 656
  • View blog
  • Posts: 2,247
  • Joined: 31-December 10

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 12:46 PM

You're opening the file to read it hence the "r+" argument. The problem with this is, if that file doesn't exist, it can't be opened. So you need to create that text file (with notepad or something else) before you run the program. Also, I would think you would be able to do all of this in one program not two.

Just to make sure I'm understanding you, you want to read data from the .txt file and write it to the .dat file? Or is it the other way around?
Was This Post Helpful? 0
  • +
  • -

#8 drowningintheC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-December 11

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 12:51 PM

Thanks for your help thus far. I really wish this came easier to me. And it's the other way around. I want to input the data into a .dat file in the first program, transfer the data to a .txt file, and in the second program, open it from the .txt file and read it. It's a really weird project. But it's what I've been asked to do. Couldn't tell you why.
Was This Post Helpful? 0
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6058
  • View blog
  • Posts: 23,496
  • Joined: 23-August 08

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 12:54 PM

Well this:

file ld.exe cannot open output file bin\Debug\InFile.exe Permission denied.



likely means that the program is already running, so it can't write over the existing file.
Was This Post Helpful? 0
  • +
  • -

#10 drowningintheC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-December 11

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 02:04 PM

Oh. Well, that's weird, since I didn't open it before I got that error. Is there any way to close the program manually? It's definitely not in an open window.
Was This Post Helpful? 0
  • +
  • -

#11 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,134
  • Joined: 07-September 06

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 02:43 PM

Well, if you are on linux you can use ps to list your currently running processes, or use top to get a larger list that is real-time (and also, IMO, more difficult to use). And then kill the process you don't want running.

If you are on windows you can use the task manager.

From the sounds of it you are currently using multiple programs to read/ write the text file. The problem here is that if one of the programs has the text file open it will also attempt to lock that file so that no other programs can open it while the first program is using it.

I would suggest only having 1 program use the file... if that makes sense.
Was This Post Helpful? 0
  • +
  • -

#12 drowningintheC  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 07-December 11

Re: Exporting data to a .txt file in C

Posted 07 December 2011 - 07:43 PM

Aha! The program was still running in the background of my computer. Once I closed it and rearranged the code a little, it ran and worked pretty well. Thanks for all the help!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1