How do I import a text file in a cabal project?












2















In app/Main.hs, I want to open a text file, "foo.txt". I know how to open a text file in a plain Haskell program. In my cabal project,



import System.IO

Main = do
contents <- readFile "foo.txt"
print $ Main.lex contents
return contents


type Keyword = String
lex :: String -> [Keyword]
lex "" =
lex x = words x


gives the error




openFile: does not exist (No such file or directory)




What do I need to change in my cabal file, or the file path or location to be able to open the file? I've tried putting it next to the output binary, and that doesn't work either.



This is my cabal file:



-- This file has been generated from package.yaml by hpack version 0.28.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: baf2fc7e230f4b4937dfd918a13fefb55b66c7a4468b24d0e3e90cad675b26d5

name: CCompiler
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/CCompiler#readme>
homepage: https://github.com/githubuser/CCompiler#readme
bug-reports: https://github.com/githubuser/CCompiler/issues
author: Author name here
maintainer: example@example.com
copyright: 2018 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
cabal-version: >= 1.10
extra-source-files:
ChangeLog.md
README.md

source-repository head
type: git
location: https://github.com/githubuser/CCompiler

library
exposed-modules:
Lib
other-modules:
Paths_CCompiler
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010

executable CCompiler-exe
main-is: Main.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010

test-suite CCompiler-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010









share|improve this question























  • How do you start your haskell program? What is its current working directory? If you use relative paths in source code, they will be resolved relative to the current working directory.

    – typetetris
    Nov 24 '18 at 12:24











  • I just do cabal run. I tried putting it everywhere though and it didn't work. I figured out what to do though.

    – michaelAdam
    Nov 24 '18 at 12:38











  • If you figured it out, write your own answer, so people understand, what your problem was.

    – typetetris
    Nov 25 '18 at 7:24
















2















In app/Main.hs, I want to open a text file, "foo.txt". I know how to open a text file in a plain Haskell program. In my cabal project,



import System.IO

Main = do
contents <- readFile "foo.txt"
print $ Main.lex contents
return contents


type Keyword = String
lex :: String -> [Keyword]
lex "" =
lex x = words x


gives the error




openFile: does not exist (No such file or directory)




What do I need to change in my cabal file, or the file path or location to be able to open the file? I've tried putting it next to the output binary, and that doesn't work either.



This is my cabal file:



-- This file has been generated from package.yaml by hpack version 0.28.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: baf2fc7e230f4b4937dfd918a13fefb55b66c7a4468b24d0e3e90cad675b26d5

name: CCompiler
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/CCompiler#readme>
homepage: https://github.com/githubuser/CCompiler#readme
bug-reports: https://github.com/githubuser/CCompiler/issues
author: Author name here
maintainer: example@example.com
copyright: 2018 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
cabal-version: >= 1.10
extra-source-files:
ChangeLog.md
README.md

source-repository head
type: git
location: https://github.com/githubuser/CCompiler

library
exposed-modules:
Lib
other-modules:
Paths_CCompiler
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010

executable CCompiler-exe
main-is: Main.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010

test-suite CCompiler-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010









share|improve this question























  • How do you start your haskell program? What is its current working directory? If you use relative paths in source code, they will be resolved relative to the current working directory.

    – typetetris
    Nov 24 '18 at 12:24











  • I just do cabal run. I tried putting it everywhere though and it didn't work. I figured out what to do though.

    – michaelAdam
    Nov 24 '18 at 12:38











  • If you figured it out, write your own answer, so people understand, what your problem was.

    – typetetris
    Nov 25 '18 at 7:24














2












2








2


1






In app/Main.hs, I want to open a text file, "foo.txt". I know how to open a text file in a plain Haskell program. In my cabal project,



import System.IO

Main = do
contents <- readFile "foo.txt"
print $ Main.lex contents
return contents


type Keyword = String
lex :: String -> [Keyword]
lex "" =
lex x = words x


gives the error




openFile: does not exist (No such file or directory)




What do I need to change in my cabal file, or the file path or location to be able to open the file? I've tried putting it next to the output binary, and that doesn't work either.



This is my cabal file:



-- This file has been generated from package.yaml by hpack version 0.28.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: baf2fc7e230f4b4937dfd918a13fefb55b66c7a4468b24d0e3e90cad675b26d5

name: CCompiler
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/CCompiler#readme>
homepage: https://github.com/githubuser/CCompiler#readme
bug-reports: https://github.com/githubuser/CCompiler/issues
author: Author name here
maintainer: example@example.com
copyright: 2018 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
cabal-version: >= 1.10
extra-source-files:
ChangeLog.md
README.md

source-repository head
type: git
location: https://github.com/githubuser/CCompiler

library
exposed-modules:
Lib
other-modules:
Paths_CCompiler
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010

executable CCompiler-exe
main-is: Main.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010

test-suite CCompiler-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010









share|improve this question














In app/Main.hs, I want to open a text file, "foo.txt". I know how to open a text file in a plain Haskell program. In my cabal project,



import System.IO

Main = do
contents <- readFile "foo.txt"
print $ Main.lex contents
return contents


type Keyword = String
lex :: String -> [Keyword]
lex "" =
lex x = words x


gives the error




openFile: does not exist (No such file or directory)




What do I need to change in my cabal file, or the file path or location to be able to open the file? I've tried putting it next to the output binary, and that doesn't work either.



This is my cabal file:



-- This file has been generated from package.yaml by hpack version 0.28.2.
--
-- see: https://github.com/sol/hpack
--
-- hash: baf2fc7e230f4b4937dfd918a13fefb55b66c7a4468b24d0e3e90cad675b26d5

name: CCompiler
version: 0.1.0.0
description: Please see the README on GitHub at <https://github.com/githubuser/CCompiler#readme>
homepage: https://github.com/githubuser/CCompiler#readme
bug-reports: https://github.com/githubuser/CCompiler/issues
author: Author name here
maintainer: example@example.com
copyright: 2018 Author name here
license: BSD3
license-file: LICENSE
build-type: Simple
cabal-version: >= 1.10
extra-source-files:
ChangeLog.md
README.md

source-repository head
type: git
location: https://github.com/githubuser/CCompiler

library
exposed-modules:
Lib
other-modules:
Paths_CCompiler
hs-source-dirs:
src
build-depends:
base >=4.7 && <5
default-language: Haskell2010

executable CCompiler-exe
main-is: Main.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
app
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010

test-suite CCompiler-test
type: exitcode-stdio-1.0
main-is: Spec.hs
other-modules:
Paths_CCompiler
hs-source-dirs:
test
ghc-options: -threaded -rtsopts -with-rtsopts=-N
build-depends:
CCompiler
, base >=4.7 && <5
default-language: Haskell2010






haskell cabal






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 24 '18 at 12:14









michaelAdammichaelAdam

495824




495824













  • How do you start your haskell program? What is its current working directory? If you use relative paths in source code, they will be resolved relative to the current working directory.

    – typetetris
    Nov 24 '18 at 12:24











  • I just do cabal run. I tried putting it everywhere though and it didn't work. I figured out what to do though.

    – michaelAdam
    Nov 24 '18 at 12:38











  • If you figured it out, write your own answer, so people understand, what your problem was.

    – typetetris
    Nov 25 '18 at 7:24



















  • How do you start your haskell program? What is its current working directory? If you use relative paths in source code, they will be resolved relative to the current working directory.

    – typetetris
    Nov 24 '18 at 12:24











  • I just do cabal run. I tried putting it everywhere though and it didn't work. I figured out what to do though.

    – michaelAdam
    Nov 24 '18 at 12:38











  • If you figured it out, write your own answer, so people understand, what your problem was.

    – typetetris
    Nov 25 '18 at 7:24

















How do you start your haskell program? What is its current working directory? If you use relative paths in source code, they will be resolved relative to the current working directory.

– typetetris
Nov 24 '18 at 12:24





How do you start your haskell program? What is its current working directory? If you use relative paths in source code, they will be resolved relative to the current working directory.

– typetetris
Nov 24 '18 at 12:24













I just do cabal run. I tried putting it everywhere though and it didn't work. I figured out what to do though.

– michaelAdam
Nov 24 '18 at 12:38





I just do cabal run. I tried putting it everywhere though and it didn't work. I figured out what to do though.

– michaelAdam
Nov 24 '18 at 12:38













If you figured it out, write your own answer, so people understand, what your problem was.

– typetetris
Nov 25 '18 at 7:24





If you figured it out, write your own answer, so people understand, what your problem was.

– typetetris
Nov 25 '18 at 7:24












2 Answers
2






active

oldest

votes


















2














add



data-dir: data


in the top section of the cabal file.



create the directory 'data' next to src and app, and put all files in there.



Make sure your cabal file also has this line



other-modules:
Paths_CCompiler


with your project's name instead of CCompiler.



My main function is now this



module Main where

import Lib
import System.IO
import Paths_CCompiler

main = do
filepath <- getDataFileName "return_2.c"
contents <- readFile filepath
print $ Lib.lex contents
return contents


Thanks to this blog post.






share|improve this answer































    0














    I understand your question to be about finding files at runtime, which you want to process and aren't packaged with your package.



    Where are severals ways, how you could find files at runtime, which aren't packaged.



    Either add an command line flag and call your executable with the absolute path of the file you want to process.



    Or implement a file chooser dialog, with e.g. gi-gtk.



    Or hard coding relative paths which isn't advisable, as they are interpreted relative to the current working directory of your process, which can be different, depending on how the program did get started.



    If you want to determine, which current working directory your program runs in, if started with cabal run, just do a litte test project with the following cabal file:



    name: test2
    build-type: Simple
    cabal-version: >= 1.10
    version: 0.0.0.1

    executable test2
    hs-source-dirs: .
    main-is: test2.hs
    build-depends:
    base
    , directory


    and the following test2.hs:



    module Main where

    import System.Directory

    main :: IO ()
    main = do
    cwd <- getCurrentDirectory
    putStrLn cwd





    share|improve this answer























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458039%2fhow-do-i-import-a-text-file-in-a-cabal-project%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      2














      add



      data-dir: data


      in the top section of the cabal file.



      create the directory 'data' next to src and app, and put all files in there.



      Make sure your cabal file also has this line



      other-modules:
      Paths_CCompiler


      with your project's name instead of CCompiler.



      My main function is now this



      module Main where

      import Lib
      import System.IO
      import Paths_CCompiler

      main = do
      filepath <- getDataFileName "return_2.c"
      contents <- readFile filepath
      print $ Lib.lex contents
      return contents


      Thanks to this blog post.






      share|improve this answer




























        2














        add



        data-dir: data


        in the top section of the cabal file.



        create the directory 'data' next to src and app, and put all files in there.



        Make sure your cabal file also has this line



        other-modules:
        Paths_CCompiler


        with your project's name instead of CCompiler.



        My main function is now this



        module Main where

        import Lib
        import System.IO
        import Paths_CCompiler

        main = do
        filepath <- getDataFileName "return_2.c"
        contents <- readFile filepath
        print $ Lib.lex contents
        return contents


        Thanks to this blog post.






        share|improve this answer


























          2












          2








          2







          add



          data-dir: data


          in the top section of the cabal file.



          create the directory 'data' next to src and app, and put all files in there.



          Make sure your cabal file also has this line



          other-modules:
          Paths_CCompiler


          with your project's name instead of CCompiler.



          My main function is now this



          module Main where

          import Lib
          import System.IO
          import Paths_CCompiler

          main = do
          filepath <- getDataFileName "return_2.c"
          contents <- readFile filepath
          print $ Lib.lex contents
          return contents


          Thanks to this blog post.






          share|improve this answer













          add



          data-dir: data


          in the top section of the cabal file.



          create the directory 'data' next to src and app, and put all files in there.



          Make sure your cabal file also has this line



          other-modules:
          Paths_CCompiler


          with your project's name instead of CCompiler.



          My main function is now this



          module Main where

          import Lib
          import System.IO
          import Paths_CCompiler

          main = do
          filepath <- getDataFileName "return_2.c"
          contents <- readFile filepath
          print $ Lib.lex contents
          return contents


          Thanks to this blog post.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 24 '18 at 12:42









          michaelAdammichaelAdam

          495824




          495824

























              0














              I understand your question to be about finding files at runtime, which you want to process and aren't packaged with your package.



              Where are severals ways, how you could find files at runtime, which aren't packaged.



              Either add an command line flag and call your executable with the absolute path of the file you want to process.



              Or implement a file chooser dialog, with e.g. gi-gtk.



              Or hard coding relative paths which isn't advisable, as they are interpreted relative to the current working directory of your process, which can be different, depending on how the program did get started.



              If you want to determine, which current working directory your program runs in, if started with cabal run, just do a litte test project with the following cabal file:



              name: test2
              build-type: Simple
              cabal-version: >= 1.10
              version: 0.0.0.1

              executable test2
              hs-source-dirs: .
              main-is: test2.hs
              build-depends:
              base
              , directory


              and the following test2.hs:



              module Main where

              import System.Directory

              main :: IO ()
              main = do
              cwd <- getCurrentDirectory
              putStrLn cwd





              share|improve this answer




























                0














                I understand your question to be about finding files at runtime, which you want to process and aren't packaged with your package.



                Where are severals ways, how you could find files at runtime, which aren't packaged.



                Either add an command line flag and call your executable with the absolute path of the file you want to process.



                Or implement a file chooser dialog, with e.g. gi-gtk.



                Or hard coding relative paths which isn't advisable, as they are interpreted relative to the current working directory of your process, which can be different, depending on how the program did get started.



                If you want to determine, which current working directory your program runs in, if started with cabal run, just do a litte test project with the following cabal file:



                name: test2
                build-type: Simple
                cabal-version: >= 1.10
                version: 0.0.0.1

                executable test2
                hs-source-dirs: .
                main-is: test2.hs
                build-depends:
                base
                , directory


                and the following test2.hs:



                module Main where

                import System.Directory

                main :: IO ()
                main = do
                cwd <- getCurrentDirectory
                putStrLn cwd





                share|improve this answer


























                  0












                  0








                  0







                  I understand your question to be about finding files at runtime, which you want to process and aren't packaged with your package.



                  Where are severals ways, how you could find files at runtime, which aren't packaged.



                  Either add an command line flag and call your executable with the absolute path of the file you want to process.



                  Or implement a file chooser dialog, with e.g. gi-gtk.



                  Or hard coding relative paths which isn't advisable, as they are interpreted relative to the current working directory of your process, which can be different, depending on how the program did get started.



                  If you want to determine, which current working directory your program runs in, if started with cabal run, just do a litte test project with the following cabal file:



                  name: test2
                  build-type: Simple
                  cabal-version: >= 1.10
                  version: 0.0.0.1

                  executable test2
                  hs-source-dirs: .
                  main-is: test2.hs
                  build-depends:
                  base
                  , directory


                  and the following test2.hs:



                  module Main where

                  import System.Directory

                  main :: IO ()
                  main = do
                  cwd <- getCurrentDirectory
                  putStrLn cwd





                  share|improve this answer













                  I understand your question to be about finding files at runtime, which you want to process and aren't packaged with your package.



                  Where are severals ways, how you could find files at runtime, which aren't packaged.



                  Either add an command line flag and call your executable with the absolute path of the file you want to process.



                  Or implement a file chooser dialog, with e.g. gi-gtk.



                  Or hard coding relative paths which isn't advisable, as they are interpreted relative to the current working directory of your process, which can be different, depending on how the program did get started.



                  If you want to determine, which current working directory your program runs in, if started with cabal run, just do a litte test project with the following cabal file:



                  name: test2
                  build-type: Simple
                  cabal-version: >= 1.10
                  version: 0.0.0.1

                  executable test2
                  hs-source-dirs: .
                  main-is: test2.hs
                  build-depends:
                  base
                  , directory


                  and the following test2.hs:



                  module Main where

                  import System.Directory

                  main :: IO ()
                  main = do
                  cwd <- getCurrentDirectory
                  putStrLn cwd






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 25 '18 at 7:23









                  typetetristypetetris

                  2,660323




                  2,660323






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458039%2fhow-do-i-import-a-text-file-in-a-cabal-project%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Costa Masnaga

                      Fotorealismo

                      Sidney Franklin