4 Replies - 178 Views - Last Post: 06 September 2019 - 10:12 AM Rate Topic: -----

#1 azsxd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-September 19

Cannot read file

Posted 06 September 2019 - 09:23 AM

Im trying to read a file but it doesn't work and I don't get any error messages. Can someone tell me what's wrong with the code?

int importFile(const char* filename, double content[], int len)

{

    FILE* fp = fopen(filename, "r");

    double input;

    int i;

    if(fp == NULL)

    {

        printf("ERROR");

        fclose(fp);

        return -1;

    }

    else

    {

        for(i = 0; i < len; i++)

        {

           if(fscanf(fp, "%lf", &input))

            {

                content[i] = input;
  

            }

        }

    }

    fclose(fp);

    return 0;

}


Is This A Good Question/Topic? 0
  • +

Replies To: Cannot read file

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15262
  • View blog
  • Posts: 61,181
  • Joined: 12-June 08

Re: Cannot read file

Posted 06 September 2019 - 09:26 AM

Explain what you mean by "doesn't work".

what does your file look like?

What are the values for the parameters?
Was This Post Helpful? 0
  • +
  • -

#3 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6818
  • View blog
  • Posts: 28,229
  • Joined: 12-December 12

Re: Cannot read file

Posted 06 September 2019 - 09:29 AM

Where and how do you call the function? What do you do with content?
Was This Post Helpful? 0
  • +
  • -

#4 azsxd   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 06-September 19

Re: Cannot read file

Posted 06 September 2019 - 10:01 AM

View Postmodi123_1, on 06 September 2019 - 09:26 AM, said:

Explain what you mean by "doesn't work".

what does your file look like?

What are the values for the parameters?


Im trying to read and write to a file, which contains numbers. When I write to the file after I've read it, the content of the file does not print. It prints zeros. filename is the name of the output file, result is the double array to save and length is the length of the array which is 50.

This is the code for writing to the file:
int exportFile(const char* filename, const double result[], int length)
{
    FILE* fp = fopen(filename, "w");
	if(fp == NULL)
	{
		printf("ERROR!");
		fclose(fp);
		return -1;
	}

	for(int i = 0; i < length; i++)
	{
		fprintf(fp, "%lf\n", result[i]);
	}

	fclose(fp);
	return 0;
}




Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg   User is offline

  • member icon

Reputation: 5761
  • View blog
  • Posts: 17,640
  • Joined: 25-December 09

Re: Cannot read file

Posted 06 September 2019 - 10:12 AM

How do you know that the file is not being read? In the code posted you never display anything so there is no way you know that the file is not being properly read.

Be careful with that fscanf() call, do you know what fsanf() can actually return (doesn't look like you do)?


Edit:

Quote

When I write to the file after I've read it, the content of the file does not print. It prints zeros.

Okay so how about showing how you're calling the read function and how you're trying to print the values/

Quote

This is the code for writing to the file:

How did you verify that you're properly writing to the file?

Edit 2:
It would also be helpful if you show the contents of the file after you try to write the information.

Jim

This post has been edited by jimblumberg: 06 September 2019 - 10:14 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1